cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject cayenne git commit: CAY-2354 DbGenerator.runGenerator must commit its connection
Date Wed, 23 Aug 2017 15:21:30 GMT
Repository: cayenne
Updated Branches:
  refs/heads/STABLE-4.0 3cd6288fe -> 9717b704d


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/9717b704
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/9717b704
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/9717b704

Branch: refs/heads/STABLE-4.0
Commit: 9717b704dbe29aea43f0fba7995ddb8ee525c921
Parents: 3cd6288
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Wed Aug 23 18:21:24 2017 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Wed Aug 23 18:21:24 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/9717b704/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/9717b704/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 f444856..bda40a7 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -22,6 +22,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


Mime
View raw message