1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.dbcp2.managed;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27
28 import javax.transaction.TransactionManager;
29 import javax.transaction.xa.XAException;
30
31 import org.apache.commons.dbcp2.ConnectionFactory;
32 import org.apache.commons.dbcp2.Constants;
33 import org.apache.commons.dbcp2.DriverConnectionFactory;
34 import org.apache.commons.dbcp2.PoolableConnection;
35 import org.apache.commons.dbcp2.PoolableConnectionFactory;
36 import org.apache.commons.dbcp2.PoolingDataSource;
37 import org.apache.commons.dbcp2.TesterDriver;
38 import org.apache.commons.pool2.SwallowedExceptionListener;
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 TestManagedConnectionCachedState {
49
50 private static final class SwallowedExceptionRecorder implements SwallowedExceptionListener {
51
52 private final List<Exception> exceptions = new ArrayList<>();
53
54 public List<Exception> getExceptions() {
55 return exceptions;
56 }
57
58 @Override
59 public void onSwallowException(final Exception e) {
60 exceptions.add(e);
61 }
62 }
63
64 private PoolingDataSource<PoolableConnection> ds;
65
66 private GenericObjectPool<PoolableConnection> pool;
67
68 private TransactionManager transactionManager;
69
70 private SwallowedExceptionRecorder swallowedExceptionRecorder;
71
72 public Connection getConnection() throws SQLException {
73 return ds.getConnection();
74 }
75
76 @BeforeEach
77 public void setUp() throws XAException {
78
79 transactionManager = new TransactionManagerImpl();
80
81
82 final Properties properties = new Properties();
83 properties.setProperty(Constants.KEY_USER, "userName");
84 properties.setProperty(Constants.KEY_PASSWORD, "password");
85 final ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);
86
87
88 final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(transactionManager, connectionFactory);
89
90
91
92 final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
93 factory.setValidationQuery("SELECT DUMMY FROM DUAL");
94 factory.setCacheState(true);
95
96
97 pool = new GenericObjectPool<>(factory);
98 factory.setPool(pool);
99
100 swallowedExceptionRecorder = new SwallowedExceptionRecorder();
101 pool.setSwallowedExceptionListener(swallowedExceptionRecorder);
102
103
104 ds = new ManagedDataSource<>(pool, xaConnectionFactory.getTransactionRegistry());
105 ds.setAccessToUnderlyingConnectionAllowed(true);
106 }
107
108 @AfterEach
109 public void tearDown() {
110 pool.close();
111 }
112
113 @Test
114 public void testConnectionCachedState() throws Exception {
115
116
117
118 transactionManager.begin();
119
120 try (final Connection conn = getConnection()) {
121
122 conn.getAutoCommit();
123
124 transactionManager.rollback();
125 }
126
127 assertEquals(0, swallowedExceptionRecorder.getExceptions().size());
128 }
129
130 }