1
package org.mariadb.jdbc;
3
import java.sql.Connection;
4
import java.sql.SQLException;
5
import java.util.Properties;
9
public class ConnectionPoolTest extends BaseTest {
11
/* For this test case to compile the following must be added to the pom.xml:
13
<groupId>commons-dbcp</groupId>
14
<artifactId>commons-dbcp</artifactId>
15
<version>1.4</version>
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);
27
Connection connection = dataSource.getConnection();
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.
38
public void testConnectionWithSimululatedApacheDBCP() throws SQLException {
40
java.sql.Driver driver = new org.mariadb.jdbc.Driver();
42
Properties props = new Properties();
43
props.put("user", mUsername);
44
props.put("password", mPassword);
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,
51
//Create 1 first connection (This is typically done in the Connection validation step in a
53
Connection connection1 = factory.createConnection();
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();
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) {
67
_connectUri = connectUri;
71
public Connection createConnection() throws SQLException {
72
return _driver.connect(_connectUri,_props);
75
protected java.sql.Driver _driver = null;
76
protected String _connectUri = null;
77
protected Properties _props = null;