diff options
author | David Gageot <david@gageot.net> | 2012-07-10 14:46:06 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-07-10 14:46:06 +0200 |
commit | 9af370fabadd77a3714272fa9f60dc4ee0928c7e (patch) | |
tree | de93e690233c44b833bafc7b5cc229576fb664f1 /sonar-core | |
parent | 264e44083a5e9c1bafb2591f2243b0dbc4450ca8 (diff) | |
download | sonarqube-9af370fabadd77a3714272fa9f60dc4ee0928c7e.tar.gz sonarqube-9af370fabadd77a3714272fa9f60dc4ee0928c7e.zip |
Make sure MyBatis is not configured to use generated keys in batch inserts
Some databases support using generated keys (mysql), some don't (Postgresql),
some make you think they do (Derby). With this check we make sure that
we catch this kind of error as early as possible
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java b/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java index 4c0d1604d6f..1b730018483 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java @@ -20,6 +20,9 @@ package org.sonar.core.persistence; import org.apache.ibatis.executor.BatchResult; +import org.apache.ibatis.executor.keygen.KeyGenerator; +import org.apache.ibatis.executor.keygen.NoKeyGenerator; +import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; @@ -102,15 +105,30 @@ public final class BatchSession implements SqlSession { } public int insert(String statement) { + makeSureGeneratedKeysAreNotUsedInBatchInserts(statement); increment(); return session.insert(statement); } public int insert(String statement, Object parameter) { + makeSureGeneratedKeysAreNotUsedInBatchInserts(statement); increment(); return session.insert(statement, parameter); } + private void makeSureGeneratedKeysAreNotUsedInBatchInserts(String statement) { + Configuration configuration = session.getConfiguration(); + if (null != configuration) { + MappedStatement mappedStatement = configuration.getMappedStatement(statement); + if (null != mappedStatement) { + KeyGenerator keyGenerator = mappedStatement.getKeyGenerator(); + if (!(keyGenerator instanceof NoKeyGenerator)) { + throw new IllegalStateException("Batch updates cannot use generated keys"); + } + } + } + } + public int update(String statement) { increment(); return session.update(statement); |