#392 - Fix for AutoCommit mode ... fixing change from #204 ... such that AutoCommitJdbcTransaction works as expected

This commit is contained in:
Robin Bygrave
2015-08-20 22:38:53 +12:00
parent 84190f3c2a
commit 2caeb4fff1
4 changed files with 81 additions and 26 deletions
@@ -14,6 +14,11 @@ public class AutoCommitJdbcTransaction extends JdbcTransaction {
super(id, explicit, connection, manager);
}
@Override
protected void checkAutoCommit(Connection connection) throws SQLException {
// do nothing as autoCommit
}
@Override
protected void performRollback() throws SQLException {
// do nothing as autoCommit
@@ -157,18 +157,26 @@ public class JdbcTransaction implements SpiTransaction {
this.batchOnCascadeMode = manager == null ? PersistBatch.NONE : manager.getPersistBatchOnCascade();
this.onQueryOnly = manager == null ? OnQueryOnly.ROLLBACK : manager.getOnQueryOnly();
this.persistenceContext = new DefaultPersistenceContext();
if (connection != null) {
this.autoCommit = connection.getAutoCommit();
if (this.autoCommit) {
connection.setAutoCommit(false);
}
}
checkAutoCommit(connection);
} catch (Exception e) {
throw new PersistenceException(e);
}
}
/**
* Overridden in AutoCommitJdbcTransaction as that expects to run/operate with autocommit true.
*/
protected void checkAutoCommit(Connection connection) throws SQLException {
if (connection != null) {
this.autoCommit = connection.getAutoCommit();
if (this.autoCommit) {
connection.setAutoCommit(false);
}
}
}
private static String deriveLogPrefix(String id) {
StringBuilder sb = new StringBuilder();
@@ -3,50 +3,85 @@ package com.avaje.ebeaninternal.server.transaction;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.Query;
import com.avaje.ebean.Transaction;
import com.avaje.ebean.config.DataSourceConfig;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool;
import com.avaje.tests.model.basic.UTDetail;
import org.junit.Assert;
import com.avaje.tests.model.basic.UTMaster;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TestAutoCommitDataSource extends BaseTestCase {
@Test
public void test() {
public void test() throws SQLException {
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.loadSettings("h2autocommit");//"pg"
dsConfig.setAutoCommit(true);
DataSourcePool pool = new DataSourcePool(null, "h2autocommit", dsConfig);
Connection connection = pool.getConnection();
assertTrue(connection.getAutoCommit());
connection.close();
ServerConfig config = new ServerConfig();
config.setName("h2autocommit");
config.loadFromProperties();
config.setDataSource(pool);
config.setDefaultServer(false);
config.setRegister(false);
config.addClass(UTMaster.class);
config.addClass(UTDetail.class);
config.setDdlGenerate(true);
config.setDdlRun(true);
config.setAutoCommitMode(true);
EbeanServer ebeanServer = EbeanServerFactory.create(config);
UTDetail detail1 = new UTDetail("one", 12, 30D);
UTDetail detail2 = new UTDetail("two", 11, 30D);
UTDetail detail3 = new UTDetail("three", 8, 30D);
Query<UTMaster> query = ebeanServer.find(UTMaster.class);
List<UTMaster> details = ebeanServer.findList(query, null);
assertEquals(0, details.size());
UTMaster bean1 = new UTMaster("one1");
UTMaster bean2 = new UTMaster("two2");
UTMaster bean3 = new UTMaster("three3");
// use a different transaction to do final query check
Transaction otherTxn = ebeanServer.createTransaction();
Transaction txn = ebeanServer.beginTransaction();
assertTrue(txn.getConnection().getAutoCommit());
try {
txn.setBatchMode(true);
ebeanServer.save(detail1);
ebeanServer.save(detail2);
ebeanServer.save(detail3);
txn.commit();
ebeanServer.save(bean1);
ebeanServer.save(bean2);
Query<UTMaster> query2 = ebeanServer.find(UTMaster.class);
details = ebeanServer.findList(query2, otherTxn);
assertEquals(2, details.size());
ebeanServer.save(bean3);
txn.rollback();
} finally {
txn.end();
}
List<UTDetail> details = ebeanServer.find(UTDetail.class).findList();
Assert.assertEquals(3, details.size());
Query<UTMaster> query3 = ebeanServer.find(UTMaster.class);
details = ebeanServer.findList(query3, otherTxn);
assertEquals(3, details.size());
}
}
@@ -29,6 +29,13 @@ public class UTMaster extends Model {
@OneToMany(cascade = CascadeType.ALL)
List<UTDetail> details;
public UTMaster() {
}
public UTMaster(String name) {
this.name = name;
}
public Integer getId() {
return id;
}