Skip to content

Commit b4ff574

Browse files
authored
Merge pull request #3579 from FOCONIS/rollbac-or-commit-before-close
Rollback/Commit raw connections before close
2 parents 11e7839 + 7643dc9 commit b4ff574

File tree

10 files changed

+23
-4
lines changed

10 files changed

+23
-4
lines changed

ebean-api/src/main/java/io/ebean/config/dbplatform/SequenceIdGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected List<Long> getMoreIds(int requestSize) {
157157
if (newIds.isEmpty()) {
158158
throw new PersistenceException("Always expecting more than 1 row from " + sql);
159159
}
160-
160+
connection.commit();
161161
return newIds;
162162

163163
} catch (SQLException e) {

ebean-core/src/main/java/io/ebeaninternal/server/core/DatabasePlatformFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ private DatabasePlatform byDatabaseName(String dbName) {
7777
*/
7878
private DatabasePlatform byDataSource(DataSource dataSource) {
7979
try (Connection connection = dataSource.getConnection()) {
80-
return byDatabaseMeta(connection.getMetaData(), connection);
80+
DatabasePlatform platform = byDatabaseMeta(connection.getMetaData(), connection);
81+
if (!connection.getAutoCommit()) {
82+
// we must roll back before close.
83+
connection.rollback();
84+
}
85+
return platform;
8186
} catch (SQLException ex) {
8287
throw new PersistenceException(ex);
8388
}

ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ private boolean checkDataSource(DatabaseBuilder.Settings config) {
262262
try (Connection connection = config.getDataSource().getConnection()) {
263263
if (connection.getAutoCommit()) {
264264
log.log(WARNING, "DataSource [{0}] has autoCommit defaulting to true!", config.getName());
265+
} else {
266+
connection.rollback();
265267
}
266268
return true;
267269
} catch (SQLException ex) {

ebean-core/src/main/java/io/ebeaninternal/server/query/CQueryPlanManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ private List<MetaQueryPlan> collectPlans(QueryPlanRequest request) {
6363
while (req.hasNext()) {
6464
req.nextCapture();
6565
}
66+
if (!connection.getAutoCommit()) {
67+
// CHECKME: commit or rollback here?
68+
// arguments for rollback: the collecting should never modify data.
69+
// if there are collectors that may copy the plan into tables, it's up to the collector to
70+
// commit the transaction.
71+
connection.rollback();
72+
}
6673
return req.plans();
6774
} catch (SQLException e) {
6875
CoreLog.log.log(ERROR, "Error during query plan collection", e);

ebean-querybean/src/test/java/org/querytest/QCustomerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public void usingConnection() throws SQLException {
442442
.usingConnection(connection)
443443
.findList();
444444

445+
connection.rollback();
445446
assertThat(foo).hasSize(1);
446447
}
447448

ebean-querybean/src/test/resources/application-test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ datasource.default=h2
1111
datasource.h2.username=sa
1212
datasource.h2.password=
1313
datasource.h2.url=jdbc:h2:mem:tests;NON_KEYWORDS=KEY,VALUE
14+
datasource.h2.closeWithinTxn=fail
1415

1516
datasource.pg.username=sa
1617
datasource.pg.password=

ebean-test/src/test/java/io/ebean/xtest/dbmigration/DbMigrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private void testVersioning() {
220220
config.setName(server().name());
221221
config.loadFromProperties(server().pluginApi().config().getProperties());
222222
config.setDataSource(server().dataSource());
223-
config.setReadOnlyDataSource(server().dataSource());
223+
config.setReadOnlyDataSource(server().readOnlyDataSource());
224224
config.setDdlGenerate(false);
225225
config.setDdlRun(false);
226226
config.setRegister(false);
@@ -292,7 +292,7 @@ private void testReservedKeywords() {
292292
config.setName(server().name());
293293
config.loadFromProperties(server().pluginApi().config().getProperties());
294294
config.setDataSource(server().dataSource());
295-
config.setReadOnlyDataSource(server().dataSource());
295+
config.setReadOnlyDataSource(server().readOnlyDataSource());
296296
config.setDdlGenerate(false);
297297
config.setDdlRun(false);
298298
config.setRegister(false);

ebean-test/src/test/java/io/ebean/xtest/internal/server/rawsql/TestRawSqlBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public void findDuplicateColumnName() throws SQLException {
266266
}
267267
}
268268
}
269+
connection.rollback();
269270
}
270271
}
271272

ebean-test/src/test/java/org/tests/basic/TestQueryUsingConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void usingConnection() throws SQLException {
3232
.findCount();
3333

3434
assertThat(count).isGreaterThan(0);
35+
connection.rollback();
3536
}
3637
}
3738

ebean-test/src/test/resources/ebean.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ datasource.db.databaseDriver=org.h2.Driver
7676
datasource.h2.username=sa
7777
datasource.h2.password=
7878
datasource.h2.url=jdbc:h2:mem:testsMem;DB_CLOSE_ON_EXIT=FALSE;NON_KEYWORDS=KEY,VALUE
79+
datasource.h2.closeWithinTxn=fail
7980
datasource.h2.poolListener=org.tests.basic.MyTestDataSourcePoolListener
8081
#datasource.h2.minConnections=1
8182
#datasource.h2.maxConnections=25

0 commit comments

Comments
 (0)