Skip to content

Commit 5602fac

Browse files
authored
Merge pull request #220 from domaframework/issue-219
Fix broken local transaction
2 parents 1c62c83 + 59056dc commit 5602fac

File tree

3 files changed

+15
-33
lines changed

3 files changed

+15
-33
lines changed

src/main/java/org/seasar/doma/jdbc/tx/LocalTransaction.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ protected void beginInternal(
202202
throw new TransactionAlreadyBegunException(id);
203203
}
204204
context = getLocalTransactionContext();
205-
context.begin((connection) -> {
206-
assertNotNull(connection);
205+
context.begin(() -> {
206+
Connection connection = JdbcUtil.getConnection(dataSource);
207207
int transactionIsolation;
208208
try {
209209
transactionIsolation = connection.getTransactionIsolation();
@@ -241,9 +241,7 @@ protected void beginInternal(
241241
* @return ローカルトランザクションコンテキスト
242242
*/
243243
protected LocalTransactionContext getLocalTransactionContext() {
244-
LocalTransactionContext context = new LocalTransactionContext(() -> {
245-
return JdbcUtil.getConnection(dataSource);
246-
});
244+
LocalTransactionContext context = new LocalTransactionContext();
247245
localTxContextHolder.set(context);
248246
return context;
249247
}

src/main/java/org/seasar/doma/jdbc/tx/LocalTransactionContext.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull;
1919

20-
import java.sql.Connection;
2120
import java.sql.Savepoint;
2221
import java.util.ArrayList;
2322
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
26-
import java.util.function.Function;
2725
import java.util.function.Supplier;
2826

2927
import org.seasar.doma.message.Message;
@@ -38,50 +36,37 @@ public class LocalTransactionContext {
3836

3937
private final Map<String, Savepoint> savepointMap = new HashMap<String, Savepoint>();
4038

41-
private final Supplier<Connection> connectionSupplier;
42-
43-
private Connection connection;
44-
4539
private LocalTransactionConnection localTxConnection;
4640

47-
private Function<Connection, LocalTransactionConnection> connectionInitializer;
41+
private Supplier<LocalTransactionConnection> localTxConnectionSupplier;
4842

4943
private String id;
5044

5145
private boolean rollbackOnly;
5246

53-
LocalTransactionContext(Supplier<Connection> connectionSupplier) {
54-
assertNotNull(connectionSupplier);
55-
this.connectionSupplier = connectionSupplier;
56-
}
57-
58-
void begin(
59-
Function<Connection, LocalTransactionConnection> connectionInitializer) {
60-
assertNotNull(connectionInitializer);
61-
id = String.valueOf(System.identityHashCode(connectionInitializer));
62-
this.connectionInitializer = connectionInitializer;
47+
void begin(Supplier<LocalTransactionConnection> localTxConnectionSupplier) {
48+
assertNotNull(localTxConnectionSupplier);
49+
id = String.valueOf(System.identityHashCode(localTxConnectionSupplier));
50+
this.localTxConnectionSupplier = localTxConnectionSupplier;
6351
}
6452

6553
void end() {
6654
id = null;
67-
connectionInitializer = null;
55+
localTxConnectionSupplier = null;
6856
}
6957

7058
LocalTransactionConnection getConnection() {
7159
if (localTxConnection == null) {
72-
if (connection == null) {
73-
connection = connectionSupplier.get();
74-
}
75-
if (connectionInitializer == null) {
60+
if (localTxConnectionSupplier == null) {
7661
throw new TransactionNotYetBegunException(Message.DOMA2048);
7762
}
78-
localTxConnection = connectionInitializer.apply(connection);
63+
localTxConnection = localTxConnectionSupplier.get();
7964
}
8065
return localTxConnection;
8166
}
8267

8368
boolean hasConnection() {
84-
return connection != null;
69+
return localTxConnection != null;
8570
}
8671

8772
Savepoint getSavepoint(String savepointName) {

src/test/java/org/seasar/doma/jdbc/tx/LocalTransactionContextTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import java.sql.SQLException;
2020
import java.sql.Savepoint;
2121

22-
import junit.framework.TestCase;
23-
2422
import org.seasar.doma.internal.jdbc.mock.MockConnection;
2523

24+
import junit.framework.TestCase;
25+
2626
/**
2727
* @author taedium
2828
*
@@ -32,8 +32,7 @@ public class LocalTransactionContextTest extends TestCase {
3232
public void testReleaseAndGetSavepoint() throws Exception {
3333
try (LocalTransactionConnection connection = new LocalTransactionConnection(
3434
new MockConnection(), Connection.TRANSACTION_READ_COMMITTED)) {
35-
LocalTransactionContext context = new LocalTransactionContext(
36-
() -> connection);
35+
LocalTransactionContext context = new LocalTransactionContext();
3736
context.addSavepoint("1", new MySavepoint("1"));
3837
context.addSavepoint("2", new MySavepoint("2"));
3938
context.addSavepoint("3", new MySavepoint("3"));

0 commit comments

Comments
 (0)