Skip to content

Commit 9346852

Browse files
authored
Add the ignoreGeneratedKeys options to improve performance (#861)
* Use default docker image for oracle database * Add the ignoreGeneratedKeys options to improve performance * Update documents
1 parent 534b1bf commit 9346852

File tree

20 files changed

+211
-6
lines changed

20 files changed

+211
-6
lines changed

docs/criteria-api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ We support the following settings:
11951195
* excludeNull
11961196
* include
11971197
* exclude
1198+
* ignoreGeneratedKeys
11981199

11991200
They are all optional.
12001201

docs/query/batch-insert.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ If annotated with ``@GeneratedValue`` at :doc:`../entity` identifier, the identi
4747

4848
You reference :ref:`identity-auto-generation` about cautionary point.
4949

50+
If you don't use auto-generated keys in your application, you can enable the `ingoreGeneratedKeys` flag.
51+
This flag may improve performance.
52+
53+
.. code-block:: java
54+
55+
@BatchInsert(ignoreGeneratedKeys = true)
56+
int[] insert(List<Employee> entities);
57+
5058
Version number
5159
----------------
5260

doma-core/src/main/java/org/seasar/doma/BatchInsert.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,12 @@
9191

9292
/** @return the output format of SQL logs. */
9393
SqlLogType sqlLog() default SqlLogType.FORMATTED;
94+
95+
/**
96+
* Whether auto-generated keys are not retrieved from the database to improve performance. If this
97+
* flag is enabled, entities won't have auto-generated keys.
98+
*
99+
* @return Whether auto-generated keys are not retrieved.
100+
*/
101+
boolean ignoreGeneratedKeys() default false;
94102
}

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/context/InsertSettings.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class InsertSettings extends Settings {
1313
private final List<PropertyMetamodel<?>> includedProperties = new ArrayList<>();
1414
private final List<PropertyMetamodel<?>> excludedProperties = new ArrayList<>();
1515

16+
private boolean ignoreGeneratedKeys;
17+
1618
/**
1719
* Returns the batch size.
1820
*
@@ -91,4 +93,23 @@ public List<PropertyMetamodel<?>> exclude() {
9193
public void exclude(PropertyMetamodel<?>... propertyMetamodels) {
9294
Collections.addAll(excludedProperties, propertyMetamodels);
9395
}
96+
97+
/**
98+
* Returns whether auto-generated keys are ignored.
99+
*
100+
* @return Whether auto-generated keys are ignored.
101+
*/
102+
public boolean getIgnoreGeneratedKeys() {
103+
return ignoreGeneratedKeys;
104+
}
105+
106+
/**
107+
* If this flag is enabled, performance may be improved. However, note that entities won't have
108+
* auto-generated keys.
109+
*
110+
* @param ignoreGeneratedKeys whether auto-generated keys are ignored
111+
*/
112+
public void setIgnoreGeneratedKeys(boolean ignoreGeneratedKeys) {
113+
this.ignoreGeneratedKeys = ignoreGeneratedKeys;
114+
}
94115
}

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/statement/EntityqlBatchInsertStatement.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ protected Command<BatchResult<ENTITY>> createCommand() {
6060
query.setSqlLogType(settings.getSqlLogType());
6161
query.setIncludedPropertyNames();
6262
query.setExcludedPropertyNames();
63+
query.setGeneratedKeysIgnored(settings.getIgnoreGeneratedKeys());
6364
query.setMessage(settings.getComment());
6465
query.prepare();
6566
BatchInsertCommand command =

doma-core/src/main/java/org/seasar/doma/jdbc/dialect/Dialect.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public interface Dialect {
9999
*/
100100
boolean supportsBatchUpdateResults();
101101

102+
default boolean supportsBatchExecutionReturningGeneratedValues() {
103+
return false;
104+
}
105+
102106
/**
103107
* Whether this object supports pessimistic locking.
104108
*

doma-core/src/main/java/org/seasar/doma/jdbc/query/AutoBatchInsertQuery.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ public class AutoBatchInsertQuery<ENTITY> extends AutoBatchModifyQuery<ENTITY>
3333

3434
protected boolean batchSupported = true;
3535

36+
protected boolean generatedKeysIgnored = false;
37+
3638
public AutoBatchInsertQuery(EntityType<ENTITY> entityType) {
3739
super(entityType);
3840
}
3941

42+
public void setGeneratedKeysIgnored(boolean generatedKeysIgnored) {
43+
this.generatedKeysIgnored = generatedKeysIgnored;
44+
}
45+
4046
@Override
4147
public void prepare() {
4248
super.prepare();
@@ -88,8 +94,10 @@ protected void prepareIdAndVersionPropertyTypes() {
8894
config, entityType, new ReservedIdProvider(config, entityType, entities.size()));
8995
generatedIdPropertyType.validateGenerationStrategy(idGenerationConfig);
9096
autoGeneratedKeysSupported =
91-
generatedIdPropertyType.isAutoGeneratedKeysSupported(idGenerationConfig);
92-
batchSupported = generatedIdPropertyType.isBatchSupported(idGenerationConfig);
97+
!generatedKeysIgnored
98+
&& generatedIdPropertyType.isAutoGeneratedKeysSupported(idGenerationConfig);
99+
batchSupported =
100+
generatedKeysIgnored || generatedIdPropertyType.isBatchSupported(idGenerationConfig);
93101
}
94102
}
95103
}

doma-processor/src/main/java/org/seasar/doma/internal/apt/annot/BatchModifyAnnot.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public abstract class BatchModifyAnnot extends AbstractAnnot {
2222
public static final String INCLUDE = "include";
2323
public static final String EXCLUDE = "exclude";
2424

25+
public static final String IGNORE_GENERATED_KEYS = "ignoreGeneratedKeys";
26+
2527
private final AnnotationValue sqlFile;
2628

2729
private final AnnotationValue queryTimeout;
@@ -38,6 +40,8 @@ public abstract class BatchModifyAnnot extends AbstractAnnot {
3840

3941
private final AnnotationValue sqlLog;
4042

43+
private final AnnotationValue ignoreGeneratedKeys;
44+
4145
BatchModifyAnnot(AnnotationMirror annotationMirror, Map<String, AnnotationValue> values) {
4246
super(annotationMirror);
4347

@@ -52,6 +56,7 @@ public abstract class BatchModifyAnnot extends AbstractAnnot {
5256
this.suppressOptimisticLockException = values.get(SUPPRESS_OPTIMISTIC_LOCK_EXCEPTION);
5357
this.include = values.get(INCLUDE);
5458
this.exclude = values.get(EXCLUDE);
59+
this.ignoreGeneratedKeys = values.get(IGNORE_GENERATED_KEYS);
5560
}
5661

5762
public AnnotationValue getSqlFile() {
@@ -86,6 +91,10 @@ public AnnotationValue getSqlLog() {
8691
return sqlLog;
8792
}
8893

94+
public AnnotationValue getIgnoreGeneratedKeys() {
95+
return ignoreGeneratedKeys;
96+
}
97+
8998
public int getQueryTimeoutValue() {
9099
Integer value = AnnotationValueUtil.toInteger(queryTimeout);
91100
if (value == null) {
@@ -133,4 +142,8 @@ public boolean getSqlFileValue() {
133142
}
134143
return value;
135144
}
145+
146+
public Boolean getIgnoreGeneratedKeysValues() {
147+
return AnnotationValueUtil.toBoolean(ignoreGeneratedKeys);
148+
}
136149
}

doma-processor/src/main/java/org/seasar/doma/internal/apt/generator/DaoImplQueryMethodGenerator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ public Void visitAutoBatchModifyQueryMeta(AutoBatchModifyQueryMeta m) {
387387
"__query.setOptimisticLockExceptionSuppressed(%1$s);%n", suppressOptimisticLockException);
388388
}
389389

390+
Boolean ignoreGeneratedKeys = m.getIgnoreGeneratedKeysValues();
391+
if (ignoreGeneratedKeys != null) {
392+
iprint("__query.setGeneratedKeysIgnored(%1$s);%n", ignoreGeneratedKeys);
393+
}
394+
390395
iprint("__query.prepare();%n");
391396
iprint(
392397
"%1$s __command = __support.getCommandImplementors().create%2$s(%3$s, __query);%n",

doma-processor/src/main/java/org/seasar/doma/internal/apt/meta/query/AutoBatchModifyQueryMeta.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public SqlLogType getSqlLogType() {
7171
return batchModifyAnnot.getSqlLogValue();
7272
}
7373

74+
public Boolean getIgnoreGeneratedKeysValues() {
75+
return batchModifyAnnot.getIgnoreGeneratedKeysValues();
76+
}
77+
7478
@Override
7579
public <R> R accept(QueryMetaVisitor<R> visitor) {
7680
return visitor.visitAutoBatchModifyQueryMeta(this);

0 commit comments

Comments
 (0)