1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.dbcp2.managed;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.fail;
25
26 import java.sql.Connection;
27 import java.sql.SQLException;
28 import java.time.Duration;
29 import java.util.Properties;
30
31 import javax.transaction.TransactionManager;
32
33 import org.apache.commons.dbcp2.ConnectionFactory;
34 import org.apache.commons.dbcp2.Constants;
35 import org.apache.commons.dbcp2.DriverConnectionFactory;
36 import org.apache.commons.dbcp2.PoolableConnection;
37 import org.apache.commons.dbcp2.PoolableConnectionFactory;
38 import org.apache.commons.dbcp2.TesterDriver;
39 import org.apache.commons.pool2.impl.GenericObjectPool;
40 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
41 import org.junit.jupiter.api.AfterEach;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44
45
46
47
48 public class TestPoolableManagedConnection {
49
50 private TransactionManager transactionManager;
51 private TransactionRegistry transactionRegistry;
52 private GenericObjectPool<PoolableConnection> pool;
53 private Connection conn;
54 private PoolableManagedConnection poolableManagedConnection;
55
56 @BeforeEach
57 public void setUp() throws Exception {
58
59 transactionManager = new TransactionManagerImpl();
60
61
62 final Properties properties = new Properties();
63 properties.setProperty(Constants.KEY_USER, "userName");
64 properties.setProperty(Constants.KEY_PASSWORD, "password");
65 final ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);
66
67
68 final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(transactionManager, connectionFactory);
69
70
71 transactionRegistry = xaConnectionFactory.getTransactionRegistry();
72
73
74 final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
75 factory.setValidationQuery("SELECT DUMMY FROM DUAL");
76 factory.setDefaultReadOnly(Boolean.TRUE);
77 factory.setDefaultAutoCommit(Boolean.TRUE);
78
79
80 pool = new GenericObjectPool<>(factory);
81 factory.setPool(pool);
82 pool.setMaxTotal(10);
83 pool.setMaxWait(Duration.ofMillis(100));
84 }
85
86 @AfterEach
87 public void tearDown() throws SQLException {
88 if (conn != null && !conn.isClosed()) {
89 conn.close();
90 }
91 if (pool != null && !pool.isClosed()) {
92 pool.close();
93 }
94 }
95
96 @Test
97 public void testManagedConnection() throws Exception {
98 assertEquals(0, pool.getNumActive());
99
100 conn = pool.borrowObject();
101 assertEquals(1, pool.getNumActive());
102
103 poolableManagedConnection = new PoolableManagedConnection(transactionRegistry, conn, pool);
104 poolableManagedConnection.close();
105
106 assertEquals(1, pool.getNumActive());
107
108 conn.close();
109 assertEquals(0, pool.getNumActive());
110 }
111
112 @Test
113 public void testPoolableConnection() throws Exception {
114
115
116 conn = pool.borrowObject();
117 assertNotNull(transactionRegistry.getXAResource(conn));
118
119 poolableManagedConnection = new PoolableManagedConnection(transactionRegistry, conn, pool);
120 poolableManagedConnection.close();
121 assertNotNull(transactionRegistry.getXAResource(conn));
122 }
123
124 @Test
125 public void testReallyClose() throws Exception {
126 assertEquals(0, pool.getNumActive());
127
128
129 conn = pool.borrowObject();
130 assertEquals(1, pool.getNumActive());
131 assertNotNull(transactionRegistry.getXAResource(conn));
132
133 poolableManagedConnection = new PoolableManagedConnection(transactionRegistry, conn, pool);
134 poolableManagedConnection.close();
135 assertNotNull(transactionRegistry.getXAResource(conn));
136 assertEquals(1, pool.getNumActive());
137
138 poolableManagedConnection.reallyClose();
139 try {
140 assertNull(transactionRegistry.getXAResource(conn));
141 fail("Transaction registry was supposed to be empty now");
142 } catch (final SQLException ignore) {
143
144 }
145 assertEquals(0, pool.getNumActive());
146 }
147 }