#2359 - Rename Transaction.getConnection() method to connection() with deprecation

This commit is contained in:
rbygrave
2021-09-08 14:17:30 +12:00
parent 632f04c76b
commit bb9532f42e
21 changed files with 36 additions and 31 deletions
@@ -538,6 +538,12 @@ public interface Transaction extends AutoCloseable {
* Examples of when a developer may wish to use the connection directly are:
* Savepoints, advanced CLOB BLOB use and advanced stored procedure calls.
*/
Connection connection();
/**
* Deprecated migrate to connection().
*/
@Deprecated
Connection getConnection();
/**
@@ -61,7 +61,7 @@ public class SimpleSequenceIdGenerator implements PlatformIdGenerator {
PreparedStatement pstmt = null;
ResultSet rset = null;
try {
c = useTxnConnection ? t.getConnection() : dataSource.getConnection();
c = useTxnConnection ? t.connection() : dataSource.getConnection();
pstmt = c.prepareStatement(sql);
rset = pstmt.executeQuery();
if (rset.next()) {
@@ -318,8 +318,8 @@ public abstract class SpiTransactionProxy implements SpiTransaction {
}
@Override
public Connection getConnection() {
return transaction.getConnection();
public Connection connection() {
return transaction.connection();
}
@Override
@@ -148,7 +148,7 @@ public final class InsertHandler extends DmlHandler {
PreparedStatement stmt = null;
ResultSet rset = null;
try {
stmt = transaction.getConnection().prepareStatement(persistRequest.getSelectLastInsertedId());
stmt = transaction.connection().prepareStatement(persistRequest.getSelectLastInsertedId());
rset = stmt.executeQuery();
setGeneratedKey(rset);
} finally {
@@ -409,7 +409,7 @@ final class CQueryBuilder {
}
query.setGeneratedSql(sql);
Connection connection = request.transaction().getConnection();
Connection connection = request.transaction().connection();
BeanDescriptor<?> desc = request.descriptor();
try {
@@ -132,7 +132,7 @@ public final class CQueryEngine {
t.logSummary(msg);
}
// ensure 'rollback' is logged if queryOnly transaction
t.getConnection();
t.connection();
// build a decent error message for the exception
String m = "Query threw SQLException:" + e.getMessage() + " Bind values:[" + bindLog + "] Query was:" + sql;
return dbPlatform.translate(m, e);
@@ -35,7 +35,7 @@ public final class DocStoreOnlyTransaction extends JdbcTransaction {
}
@Override
public Connection getConnection() {
public Connection connection() {
throw new RuntimeException("not supported on DocStoreTransaction");
}
@@ -503,7 +503,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
* Return the underlying connection for public use.
*/
@Override
public Connection getConnection() {
public Connection connection() {
return getInternalConnection();
}
@@ -886,7 +886,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
* Return the underlying connection for public use.
*/
@Override
public Connection getConnection() {
public Connection connection() {
queryOnly = false;
return getInternalConnection();
}
@@ -288,7 +288,7 @@ final class NoTransaction implements SpiTransaction {
}
@Override
public Connection getConnection() {
public Connection connection() {
return null;
}
@@ -35,7 +35,7 @@ abstract class TransactionFactory {
*/
final SpiTransaction setIsolationLevel(SpiTransaction t, boolean explicit, int isolationLevel) {
if (isolationLevel > -1) {
Connection connection = t.getConnection();
Connection connection = t.connection();
try {
connection.setTransactionIsolation(isolationLevel);
} catch (SQLException e) {
@@ -31,11 +31,11 @@ public class TestBatchOnCascadeExceptionHandling extends BaseTestCase {
try {
EBasicWithUniqueCon v2 = createEntityWithName("conflict", "after");
txn.flush();
Savepoint sp = txn.getConnection().setSavepoint();
Savepoint sp = txn.connection().setSavepoint();
try {
server().save(v2); // unique key violation
} catch (PersistenceException e) {
txn.getConnection().rollback(sp);
txn.connection().rollback(sp);
EBasicWithUniqueCon conflicting = server().find(EBasicWithUniqueCon.class).where().eq("name", "conflict").findOne();
assertThat(conflicting).isNotNull();
@@ -51,7 +51,7 @@ public class TestGeneratedKeys extends BaseTestCase {
private long readSequenceValue(Transaction tx, String sequence) throws SQLException {
Statement stm = null;
try {
stm = tx.getConnection().createStatement();
stm = tx.connection().createStatement();
ResultSet rs = stm.executeQuery("select currval('" + sequence + "')");
rs.next();
@@ -97,7 +97,7 @@ public class TestInsertDuplicateKey extends BaseTestCase {
// typically we would use transaction.commitAndContinue()
// ... this is a rollback and continue type scenario
// ... more sensible to use a second transaction that do this
DB.getDefault().currentTransaction().getConnection().rollback();
DB.getDefault().currentTransaction().connection().rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
@@ -47,7 +47,7 @@ public class SlowDownEBasic implements Trigger {
return;
}
triggerInstalled = true;
try (Transaction txn = DB.beginTransaction(); Statement stmt = txn.getConnection().createStatement()) {
try (Transaction txn = DB.beginTransaction(); Statement stmt = txn.connection().createStatement()) {
stmt.execute("CREATE TRIGGER SLOW_DOWN_E_BASIC BEFORE SELECT ON e_basic " + "CALL \""
+ SlowDownEBasic.class.getName() + "\"");
@@ -51,7 +51,7 @@ public class TestRawSqlNamedParams extends BaseTestCase {
Transaction transaction = DB.beginTransaction();
try {
if ("MariaDB connector/J".equals(transaction.getConnection().getMetaData().getDriverName())) {
if ("MariaDB connector/J".equals(transaction.connection().getMetaData().getDriverName())) {
return; // MariaDb only supports callable statements in the form "? = call function x(?)"
}
CallableSql callableSql = DB.createCallableSql("set @total = 0");
@@ -2,7 +2,6 @@ package org.tests.rawsql;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.FetchConfig;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import io.ebean.Transaction;
@@ -29,7 +28,7 @@ public class TestRawSqlWithResultSet extends BaseTestCase {
// Transaction supplies our jdbc Connection
Transaction txn = DB.beginTransaction();
try {
pstmt = txn.getConnection().prepareStatement("select id, name, billing_address_id from o_customer");
pstmt = txn.connection().prepareStatement("select id, name, billing_address_id from o_customer");
// ResultSet will be closed by Ebean
ResultSet resultSet = pstmt.executeQuery();
@@ -45,10 +45,10 @@ public class TimezoneTests {
private void fetch() throws SQLException {
try (
Transaction transaction = DB.beginTransaction();
Connection connection = transaction.getConnection();
PreparedStatement statement = connection.prepareStatement("select * from tztest");
ResultSet resultSet = statement.executeQuery()) {
Transaction transaction = DB.beginTransaction();
Connection connection = transaction.connection();
PreparedStatement statement = connection.prepareStatement("select * from tztest");
ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
System.out.println(" zone:" + resultSet.getString("zone"));
@@ -79,9 +79,9 @@ public class TimezoneTests {
}
try (
Transaction transaction = DB.beginTransaction();
Connection connection = transaction.getConnection();
PreparedStatement statement = connection.prepareStatement(insert)) {
Transaction transaction = DB.beginTransaction();
Connection connection = transaction.connection();
PreparedStatement statement = connection.prepareStatement(insert)) {
statement.setString(1, zone);
statement.setTimestamp(2, nowTs);
statement.setTimestamp(3, nowTs);
@@ -26,7 +26,7 @@ public class TestSavepoint extends BaseTestCase {
try (Transaction transaction = DB.beginTransaction()) {
Connection connection = transaction.getConnection();
Connection connection = transaction.connection();
Savepoint savepoint = connection.setSavepoint();
basicVer.setOther("changeOther");
@@ -22,7 +22,7 @@ public class TestTransactionCallback extends BaseTestCase {
public void test_commitAndRollback() {
try (Transaction txn = DB.beginTransaction()) {
DB.register(new MyCallback());
txn.getConnection(); // Ebean assumes writes have occurred
txn.connection(); // Ebean assumes writes have occurred
txn.commit();
}
@@ -45,14 +45,14 @@ public class TestTransactionalRequiresNew extends BaseTestCase {
void doOuter() {
outerTxn = DB.currentTransaction();
log.info("outer before ...{}", outerTxn);
outerConn = outerTxn.getConnection();
outerConn = outerTxn.connection();
new InTransactionalWithRequiresNew().doInner();
// restore the outerTxn
Transaction current = DB.currentTransaction();
log.info("outer after ...{}", current);
assertSame(outerConn, current.getConnection());
assertSame(outerConn, current.connection());
}
}
@@ -64,7 +64,7 @@ public class TestTransactionalRequiresNew extends BaseTestCase {
Transaction innerTxn = DB.currentTransaction();
log.info("inner ...{} {}", innerTxn);
Connection connection = innerTxn.getConnection();
Connection connection = innerTxn.connection();
assertNotSame(connection, outerConn);
}
}