~maria-captains/mariadb-java-client/trunk

« back to all changes in this revision

Viewing changes to src/test/java/org/mariadb/jdbc/ConnectionPoolTest.java

  • Committer: Massimo Siani
  • Date: 2014-10-21 16:46:49 UTC
  • mfrom: (520.2.1 CONJ-114)
  • Revision ID: massimo.siani@skysql.com-20141021164649-1z8wi6wi3b79a1gn
Fix for CONJ-114: Revert CONJ-89

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.mariadb.jdbc;
 
2
 
 
3
import java.sql.Connection;
 
4
import java.sql.SQLException;
 
5
import java.util.Properties;
 
6
 
 
7
import org.junit.Test;
 
8
 
 
9
public class ConnectionPoolTest extends BaseTest {
 
10
    
 
11
    /* For this test case to compile the following must be added to the pom.xml:
 
12
       <dependency>
 
13
         <groupId>commons-dbcp</groupId>
 
14
         <artifactId>commons-dbcp</artifactId>
 
15
         <version>1.4</version>
 
16
      </dependency>
 
17
     */
 
18
    @Test
 
19
    public void testConnectionWithApacheDBCP() throws SQLException {
 
20
        org.apache.commons.dbcp.BasicDataSource dataSource;
 
21
        dataSource = new org.apache.commons.dbcp.BasicDataSource();
 
22
        dataSource.setUrl(connU);
 
23
        dataSource.setUsername(mUsername);
 
24
        dataSource.setPassword(mPassword);
 
25
        dataSource.setMaxActive(2);
 
26
        
 
27
        Connection connection = dataSource.getConnection();
 
28
        
 
29
        connection.close();
 
30
        dataSource.close();
 
31
    }
 
32
    
 
33
    /**
 
34
     * This test case simulates how the Apache DBCP connection pools works. It is written so it
 
35
     * should compile without Apache DBCP but still show the problem.
 
36
     */
 
37
    @Test
 
38
    public void testConnectionWithSimululatedApacheDBCP() throws SQLException {
 
39
        
 
40
        java.sql.Driver driver = new org.mariadb.jdbc.Driver();
 
41
 
 
42
        Properties props = new Properties();
 
43
        props.put("user", mUsername);
 
44
        props.put("password", mPassword);
 
45
        
 
46
        //A connection pool typically has a connection factor that stored everything needed to
 
47
        //create a Connection. Here I create a factory that stores URL, username and password.
 
48
        SimulatedDriverConnectionFactory factory = new SimulatedDriverConnectionFactory(driver,
 
49
            connU, props);
 
50
        
 
51
        //Create 1 first connection (This is typically done in the Connection validation step in a
 
52
        //connection pool)
 
53
        Connection connection1 = factory.createConnection();
 
54
        
 
55
        //Create another connection to make sure we can access the database. This is typically the
 
56
        //Connection that is exposed to the user of the connection pool
 
57
        Connection connection2 = factory.createConnection();
 
58
        
 
59
        connection1.close();
 
60
        connection2.close();
 
61
    }
 
62
    
 
63
    /** This class is a simulated version of org.apache.commons.dbcp.DriverConnectionFactory */
 
64
    private static class SimulatedDriverConnectionFactory {
 
65
        public SimulatedDriverConnectionFactory(java.sql.Driver driver, String connectUri, Properties props) {
 
66
            _driver = driver;
 
67
            _connectUri = connectUri;
 
68
            _props = props;
 
69
        }
 
70
 
 
71
        public Connection createConnection() throws SQLException {
 
72
            return _driver.connect(_connectUri,_props);
 
73
        }
 
74
 
 
75
        protected java.sql.Driver _driver = null;
 
76
        protected String _connectUri = null;
 
77
        protected Properties _props = null;
 
78
    }
 
79
}