Repository: cayenne
Updated Branches:
  refs/heads/master 863c23e66 -> 4684f56b4


CAY-2354 DbGenerator.runGenerator must commit its connection


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/4684f56b
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/4684f56b
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/4684f56b

Branch: refs/heads/master
Commit: 4684f56b4df9463d2e2d09592af318cdb3b2328e
Parents: 863c23e
Author: Nikita Timofeev <stari...@gmail.com>
Authored: Wed Aug 23 18:21:24 2017 +0300
Committer: Nikita Timofeev <stari...@gmail.com>
Committed: Wed Aug 23 18:22:10 2017 +0300

----------------------------------------------------------------------
 .../org/apache/cayenne/access/DbGenerator.java  | 91 +++++++++++---------
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |  1 +
 2 files changed, 50 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/4684f56b/cayenne-server/src/main/java/org/apache/cayenne/access/DbGenerator.java
----------------------------------------------------------------------
diff --git 
a/cayenne-server/src/main/java/org/apache/cayenne/access/DbGenerator.java 
b/cayenne-server/src/main/java/org/apache/cayenne/access/DbGenerator.java
index 0f3a467..d49f839 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/DbGenerator.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/DbGenerator.java
@@ -268,63 +268,70 @@ public class DbGenerator {
        public void runGenerator(DataSource ds) throws Exception {
                this.failures = null;
 
-               try (Connection connection = ds.getConnection();) {
-
-                       // drop tables
-                       if (shouldDropTables) {
-                               ListIterator<DbEntity> it = 
dbEntitiesInInsertOrder.listIterator(dbEntitiesInInsertOrder.size());
-                               while (it.hasPrevious()) {
-                                       DbEntity ent = it.previous();
-                                       for (String statement : 
dropTables.get(ent.getName())) {
-                                               safeExecute(connection, 
statement);
+               try (Connection connection = ds.getConnection()) {
+                       // force connection to autocommit, see CAY-2354
+                       boolean autoCommit = connection.getAutoCommit();
+                       connection.setAutoCommit(true);
+                       try {
+                               // drop tables
+                               if (shouldDropTables) {
+                                       ListIterator<DbEntity> it = 
dbEntitiesInInsertOrder.listIterator(dbEntitiesInInsertOrder.size());
+                                       while (it.hasPrevious()) {
+                                               DbEntity ent = it.previous();
+                                               for (String statement : 
dropTables.get(ent.getName())) {
+                                                       safeExecute(connection, 
statement);
+                                               }
                                        }
                                }
-                       }
 
-                       // create tables
-                       List<String> createdTables = new ArrayList<>();
-                       if (shouldCreateTables) {
-                               for (final DbEntity ent : 
dbEntitiesInInsertOrder) {
+                               // create tables
+                               List<String> createdTables = new ArrayList<>();
+                               if (shouldCreateTables) {
+                                       for (final DbEntity ent : 
dbEntitiesInInsertOrder) {
 
-                                       // only create missing tables
+                                               // only create missing tables
 
-                                       safeExecute(connection, 
createTables.get(ent.getName()));
-                                       createdTables.add(ent.getName());
+                                               safeExecute(connection, 
createTables.get(ent.getName()));
+                                               
createdTables.add(ent.getName());
+                                       }
                                }
-                       }
 
-                       // create FK
-                       if (shouldCreateTables && shouldCreateFKConstraints) {
-                               for (DbEntity ent : dbEntitiesInInsertOrder) {
+                               // create FK
+                               if (shouldCreateTables && 
shouldCreateFKConstraints) {
+                                       for (DbEntity ent : 
dbEntitiesInInsertOrder) {
 
-                                       if 
(createdTables.contains(ent.getName())) {
-                                               List<String> fks = 
createConstraints.get(ent.getName());
-                                               for (String fk : fks) {
-                                                       safeExecute(connection, 
fk);
+                                               if 
(createdTables.contains(ent.getName())) {
+                                                       List<String> fks = 
createConstraints.get(ent.getName());
+                                                       for (String fk : fks) {
+                                                               
safeExecute(connection, fk);
+                                                       }
                                                }
                                        }
                                }
-                       }
 
-                       // drop PK
-                       if (shouldDropPKSupport) {
-                               List<String> dropAutoPKSQL = 
getAdapter().getPkGenerator().dropAutoPkStatements(
-                                               dbEntitiesRequiringAutoPK);
-                               for (final String sql : dropAutoPKSQL) {
-                                       safeExecute(connection, sql);
+                               // drop PK
+                               if (shouldDropPKSupport) {
+                                       List<String> dropAutoPKSQL = 
getAdapter().getPkGenerator().dropAutoPkStatements(
+                                                       
dbEntitiesRequiringAutoPK);
+                                       for (final String sql : dropAutoPKSQL) {
+                                               safeExecute(connection, sql);
+                                       }
                                }
-                       }
 
-                       // create pk
-                       if (shouldCreatePKSupport) {
-                               List<String> createAutoPKSQL = 
getAdapter().getPkGenerator().createAutoPkStatements(
-                                               dbEntitiesRequiringAutoPK);
-                               for (final String sql : createAutoPKSQL) {
-                                       safeExecute(connection, sql);
+                               // create pk
+                               if (shouldCreatePKSupport) {
+                                       List<String> createAutoPKSQL = 
getAdapter().getPkGenerator().createAutoPkStatements(
+                                                       
dbEntitiesRequiringAutoPK);
+                                       for (final String sql : 
createAutoPKSQL) {
+                                               safeExecute(connection, sql);
+                                       }
                                }
-                       }
 
-                       new DbGeneratorPostprocessor().execute(connection, 
getAdapter());
+                               new 
DbGeneratorPostprocessor().execute(connection, getAdapter());
+                       } finally {
+                               // restore connection autocommit state in case 
it will be recycled in some underlying pool
+                               connection.setAutoCommit(autoCommit);
+                       }
                }
        }
 
@@ -336,7 +343,7 @@ public class DbGenerator {
         */
        protected boolean safeExecute(Connection connection, String sql) throws 
SQLException {
 
-               try (Statement statement = connection.createStatement();) {
+               try (Statement statement = connection.createStatement()) {
                        jdbcEventLogger.log(sql);
                        statement.execute(sql);
                        return true;

http://git-wip-us.apache.org/repos/asf/cayenne/blob/4684f56b/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt 
b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index 0bf60cf..39e466a 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -35,6 +35,7 @@ CAY-2347 cdbimport: can't get all relationships on the first 
pass
 CAY-2349 Cache issue: 'SelectQuery' with prefetches loses relationships
 CAY-2350 Expression: NotIn with empty collection returns empty result
 CAY-2353 Broken paginated column select with only one entity in the result
+CAY-2354 DbGenerator.runGenerator must commit its connection
 
 ----------------------------------
 Release: 4.0.B1

Reply via email to