Skip to content

Commit 199d0e0

Browse files
committed
Added closeConnection property to ManagedTransactionFactory to avoid closing managed connections for some application servers.
1 parent 6ae075f commit 199d0e0

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

doc/en/MyBatis-3-User-Guide.doc

3 KB
Binary file not shown.

doc/en/MyBatis-3-User-Guide.pdf

538 Bytes
Binary file not shown.

src/main/java/org/apache/ibatis/transaction/managed/ManagedTransaction.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ public class ManagedTransaction implements Transaction {
1212
private static final Log log = LogFactory.getLog(ManagedTransaction.class);
1313

1414
private Connection connection;
15+
private boolean closeConnection;
1516

16-
public ManagedTransaction(Connection connection) {
17+
public ManagedTransaction(Connection connection, boolean closeConnection) {
1718
this.connection = connection;
19+
this.closeConnection = closeConnection;
1820
}
1921

2022
public Connection getConnection() {
@@ -30,11 +32,13 @@ public void rollback() throws SQLException {
3032
}
3133

3234
public void close() throws SQLException {
33-
try {
34-
if (connection != null) connection.close();
35-
} catch (SQLException e) {
36-
// Log and ignore. Nothing more that should be done here.
37-
log.error(e.getMessage(), e);
35+
if (closeConnection) {
36+
try {
37+
if (connection != null) connection.close();
38+
} catch (SQLException e) {
39+
// Log and ignore. Nothing more that should be done here.
40+
log.error(e.getMessage(), e);
41+
}
3842
}
3943
}
4044

src/main/java/org/apache/ibatis/transaction/managed/ManagedTransactionFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88

99
public class ManagedTransactionFactory implements TransactionFactory {
1010

11+
private boolean closeConnection = true;
12+
1113
public void setProperties(Properties props) {
14+
if (props != null) {
15+
String closeConnectionProperty = props.getProperty("closeConnection");
16+
if (closeConnectionProperty != null) {
17+
closeConnection = Boolean.valueOf(closeConnectionProperty);
18+
}
19+
}
1220
}
1321

1422
public Transaction newTransaction(Connection conn, boolean autoCommit) {
1523
// Silently ignores autocommit, as managed transactions are entirely
1624
// controlled by an external manager. It's silently ignored so that
1725
// code remains portable between managed and unmanaged configurations.
18-
return new ManagedTransaction(conn);
26+
return new ManagedTransaction(conn, closeConnection);
1927
}
2028
}

src/test/java/org/apache/ibatis/transaction/managed/ManagedTransactionFactoryTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,24 @@ public void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedCon
4040
mockery.assertIsSatisfied();
4141
}
4242

43+
44+
@Test
45+
public void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnectionsAndDoesNotCloseConnection() throws Exception {
46+
mockery.checking(new Expectations() {
47+
{
48+
}
49+
});
50+
51+
TransactionFactory tf = new ManagedTransactionFactory();
52+
Properties props = new Properties();
53+
props.setProperty("closeConnection","false");
54+
tf.setProperties(props);
55+
Transaction tx = tf.newTransaction(conn, false);
56+
assertEquals(conn, tx.getConnection());
57+
tx.commit();
58+
tx.rollback();
59+
tx.close();
60+
mockery.assertIsSatisfied();
61+
}
62+
4363
}

0 commit comments

Comments
 (0)