#415 - ENH: Add ExplicitJdbcTransaction ... so like AutoCommitJdbcTransaction but using explicit statements to commit and rollback

This commit is contained in:
Robin Bygrave committed 2015-09-18 22:25:19 +12:00
1 parent 06b5ebc14e
commit 054f7dc86f
5 files changed
+209 -1

No files matched your search

@@ -0,0 +1,45 @@
package com.avaje.ebeaninternal.server.transaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* This only works for Postgres and H2 (and doesn't work for Oracle).
*
* Uses explicit begin statement to start the transactions.
*/
public class ExplicitJdbcTransaction extends JdbcTransaction {
public ExplicitJdbcTransaction(String id, boolean explicit, Connection connection, TransactionManager manager) {
super(id, explicit, connection, manager);
}
@Override
protected void checkAutoCommit(Connection connection) throws SQLException {
// begin the transaction explicitly
executeStatement("begin");
}
@Override
protected void performRollback() throws SQLException {
// Postgres needs this explicit rollback statement when used with AutoCommit=true
executeStatement("rollback");
}
@Override
protected void performCommit() throws SQLException {
// Postgres needs this explicit commit statement when used with AutoCommit=true
executeStatement("commit");
}
private void executeStatement(String statement) throws SQLException {
PreparedStatement stmt = connection.prepareStatement(statement);
try {
stmt.execute();
} finally {
stmt.close();
}
}
}
@@ -0,0 +1,32 @@
package com.avaje.ebeaninternal.server.transaction;
import com.avaje.ebean.BackgroundExecutor;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebeaninternal.api.SpiTransaction;
import com.avaje.ebeaninternal.server.cluster.ClusterManager;
import com.avaje.ebeaninternal.server.core.BootupClasses;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager;
import java.sql.Connection;
/**
* TransactionManager where the transactions start with explicit "begin" statement.
*/
public class ExplicitTransactionManager extends TransactionManager {
public ExplicitTransactionManager(ClusterManager clusterManager, BackgroundExecutor backgroundExecutor,
ServerConfig config, BeanDescriptorManager descMgr, BootupClasses bootupClasses) {
super(clusterManager, backgroundExecutor, config, descMgr, bootupClasses);
}
/**
* Create a ExplicitJdbcTransaction.
*/
@Override
protected SpiTransaction createTransaction(boolean explicit, Connection c, long id) {
return new ExplicitJdbcTransaction(prefix + id, explicit, c, this);
}
}