View Javadoc
1   /*
2   
3     Licensed to the Apache Software Foundation (ASF) under one or more
4     contributor license agreements.  See the NOTICE file distributed with
5     this work for additional information regarding copyright ownership.
6     The ASF licenses this file to You under the Apache License, Version 2.0
7     (the "License"); you may not use this file except in compliance with
8     the License.  You may obtain a copy of the License at
9   
10        http://www.apache.org/licenses/LICENSE-2.0
11  
12     Unless required by applicable law or agreed to in writing, software
13     distributed under the License is distributed on an "AS IS" BASIS,
14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15     See the License for the specific language governing permissions and
16     limitations under the License.
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   * TestSuite for BasicManagedDataSource when using a
40   * DataSourceXAConnectionFactory (configured from a XADataSource)
41   */
42  public class TestDataSourceXAConnectionFactory extends TestBasicDataSource {
43  
44      /**
45       * Delegates everything to the BasicDataSource (ds field), except for
46       * getXAConnection which creates a BasicXAConnection.
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                  // both zero and 2-arg signatures
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       * JIRA: DBCP-355
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()); // stays idle in the pool
105         conn2.close();
106         assertEquals(1, closeCounter.get()); // can't have 2 idle ones
107         bmds.close();
108         assertEquals(2, closeCounter.get());
109     }
110 
111 }
112