Skip to content

Commit 18aebbf

Browse files
authored
Make LocalTransactionManager more useful (#609)
* Add convenient constructors to LocalTransactionManager * Provide LocalTransaction from LocalTransactionManager * Add javadoc comments
1 parent 793d37c commit 18aebbf

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/tx/LocalTransactionManager.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,82 @@
44

55
import java.util.function.Supplier;
66
import org.seasar.doma.DomaNullPointerException;
7+
import org.seasar.doma.jdbc.JdbcLogger;
78

8-
/** A transaction manager for local transactions. */
9+
/**
10+
* A transaction manager for local transactions.
11+
*
12+
* <p>This instance is thread safe.
13+
*/
914
public class LocalTransactionManager implements TransactionManager {
1015

1116
protected final LocalTransaction transaction;
1217

18+
/**
19+
* Create an instance.
20+
*
21+
* @param transaction the transaction
22+
* @throws DomaNullPointerException if the {@code transaction} is {@code null}
23+
*/
1324
public LocalTransactionManager(LocalTransaction transaction) {
1425
if (transaction == null) {
1526
throw new DomaNullPointerException("transaction");
1627
}
1728
this.transaction = transaction;
1829
}
1930

31+
/**
32+
* Create an instance.
33+
*
34+
* @param dataSource the data source
35+
* @param jdbcLogger the logger
36+
* @throws DomaNullPointerException if any of the parameters are {@code null}
37+
*/
38+
public LocalTransactionManager(LocalTransactionDataSource dataSource, JdbcLogger jdbcLogger) {
39+
if (dataSource == null) {
40+
throw new DomaNullPointerException("dataSource");
41+
}
42+
if (jdbcLogger == null) {
43+
throw new DomaNullPointerException("jdbcLogger");
44+
}
45+
this.transaction = dataSource.getLocalTransaction(jdbcLogger);
46+
}
47+
48+
/**
49+
* Create an instance.
50+
*
51+
* @param dataSource the data source
52+
* @param jdbcLogger the logger
53+
* @param isolationLevel the default transaction isolation level
54+
* @throws DomaNullPointerException if any of the parameters are {@code null}
55+
*/
56+
public LocalTransactionManager(
57+
LocalTransactionDataSource dataSource,
58+
JdbcLogger jdbcLogger,
59+
TransactionIsolationLevel isolationLevel) {
60+
if (dataSource == null) {
61+
throw new DomaNullPointerException("dataSource");
62+
}
63+
if (jdbcLogger == null) {
64+
throw new DomaNullPointerException("jdbcLogger");
65+
}
66+
if (isolationLevel == null) {
67+
throw new DomaNullPointerException("isolationLevel");
68+
}
69+
this.transaction = dataSource.getLocalTransaction(jdbcLogger, isolationLevel);
70+
}
71+
72+
/**
73+
* Returns the transaction.
74+
*
75+
* <p>This method is mainly used in test code.
76+
*
77+
* @return the transaction
78+
*/
79+
public LocalTransaction getTransaction() {
80+
return transaction;
81+
}
82+
2083
@Override
2184
public void required(Runnable block) {
2285
if (block == null) {

doma-core/src/test/java/org/seasar/doma/jdbc/tx/LocalTransactionManagerTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.seasar.doma.jdbc.tx;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
45

56
import org.junit.jupiter.api.BeforeEach;
67
import org.junit.jupiter.api.Test;
@@ -132,4 +133,23 @@ public void testNotSupported_in_tx() {
132133
log.append(LocalTransactionManagerTest.counter);
133134
assertEquals("01110", log.toString());
134135
}
136+
137+
@Test
138+
public void testConstructor1() {
139+
LocalTransactionManager manager = new LocalTransactionManager(transaction);
140+
assertNotNull(manager);
141+
}
142+
143+
@Test
144+
public void testConstructor2() {
145+
LocalTransactionManager manager = new LocalTransactionManager(dataSource, jdbcLogger);
146+
assertNotNull(manager);
147+
}
148+
149+
@Test
150+
public void testConstructor3() {
151+
LocalTransactionManager manager =
152+
new LocalTransactionManager(dataSource, jdbcLogger, TransactionIsolationLevel.SERIALIZABLE);
153+
assertNotNull(manager);
154+
}
135155
}

0 commit comments

Comments
 (0)