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.lang.reflect.InvocationHandler;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Proxy;
26 import java.sql.Connection;
27 import java.sql.SQLException;
28 import java.util.concurrent.atomic.AtomicInteger;
29
30 import javax.sql.XAConnection;
31 import javax.sql.XADataSource;
32
33 import org.apache.commons.dbcp2.TestBasicDataSource;
34 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41
42 public class TestDataSourceXAConnectionFactory extends TestBasicDataSource {
43
44
45
46
47
48 public class XADataSourceHandle implements InvocationHandler {
49
50 protected XAConnection getXAConnection() throws SQLException {
51 return new TesterBasicXAConnection(ds.getConnection(), closeCounter);
52 }
53
54 @Override
55 public Object invoke(final Object proxy, final Method method, final Object[] args)
56 throws Throwable {
57 final String methodName = method.getName();
58 if (methodName.equals("hashCode")) {
59 return System.identityHashCode(proxy);
60 }
61 if (methodName.equals("equals")) {
62 return proxy == args[0];
63 }
64 if (methodName.equals("getXAConnection")) {
65
66 return getXAConnection();
67 }
68 try {
69 return method.invoke(ds, args);
70 } catch (final InvocationTargetException e) {
71 throw e.getTargetException();
72 }
73 }
74 }
75
76 protected BasicManagedDataSource bmds;
77
78 public final AtomicInteger closeCounter = new AtomicInteger();
79
80 @Override
81 @BeforeEach
82 public void setUp() throws Exception {
83 super.setUp();
84 bmds = new BasicManagedDataSource();
85 bmds.setTransactionManager(new TransactionManagerImpl());
86 bmds.setXADataSource("notnull");
87 final XADataSourceHandle handle = new XADataSourceHandle();
88 final XADataSource xads = (XADataSource) Proxy.newProxyInstance(
89 XADataSourceHandle.class.getClassLoader(),
90 new Class[] { XADataSource.class }, handle);
91 bmds.setXaDataSourceInstance(xads);
92 }
93
94
95
96
97 @Test
98 public void testPhysicalClose() throws Exception {
99 bmds.setMaxIdle(1);
100 final Connection conn1 = bmds.getConnection();
101 final Connection conn2 = bmds.getConnection();
102 closeCounter.set(0);
103 conn1.close();
104 assertEquals(0, closeCounter.get());
105 conn2.close();
106 assertEquals(1, closeCounter.get());
107 bmds.close();
108 assertEquals(2, closeCounter.get());
109 }
110
111 }
112