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.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.sql.Connection;
24  import java.sql.SQLException;
25  
26  import javax.transaction.xa.XAResource;
27  
28  import org.apache.geronimo.transaction.manager.TransactionImpl;
29  import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * TestSuite for {@link TransactionContext}.
34   */
35  public class TestTransactionContext {
36  
37      /**
38       * Transaction that always fails enlistResource.
39       */
40      private static final class UncooperativeTransaction extends TransactionImpl {
41  
42          public UncooperativeTransaction() {
43              super(null, null);
44          }
45  
46          @Override
47          public synchronized boolean enlistResource(final XAResource xaRes) {
48              return false;
49          }
50      }
51  
52      /**
53       * JIRA: DBCP-428
54       */
55      @Test
56      public void testSetSharedConnectionEnlistFailure() throws Exception {
57          try (final BasicManagedDataSource basicManagedDataSource = new BasicManagedDataSource()) {
58              basicManagedDataSource.setTransactionManager(new TransactionManagerImpl());
59              basicManagedDataSource.setDriverClassName("org.apache.commons.dbcp2.TesterDriver");
60              basicManagedDataSource.setUrl("jdbc:apache:commons:testdriver");
61              basicManagedDataSource.setUsername("userName");
62              basicManagedDataSource.setPassword("password");
63              basicManagedDataSource.setMaxIdle(1);
64              try (final Connection conn = basicManagedDataSource.getConnection()) {
65                  assertTrue(conn instanceof ManagedConnection);
66                  final UncooperativeTransaction transaction = new UncooperativeTransaction();
67                  final TransactionContext transactionContext = new TransactionContext(basicManagedDataSource.getTransactionRegistry(), transaction);
68                  assertThrows(SQLException.class, () -> transactionContext.setSharedConnection(conn));
69              }
70          }
71      }
72  
73  }
74