Java Code Examples for com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

The following examples show how to use com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: r-course   Source File: ConnectionRegressionTest.java    License: MIT License 6 votes vote down vote up
/**
 * Tests fix for BUG#62452 - NPE thrown in JDBC4MySQLPooledException when statement is closed.
 * 
 * @throws Exception
 */
public void testBug62452() throws Exception {
    PooledConnection con = null;

    MysqlConnectionPoolDataSource pds = new MysqlConnectionPoolDataSource();
    pds.setUrl(dbUrl);
    con = pds.getPooledConnection();
    assertTrue(con instanceof JDBC4MysqlPooledConnection);
    testBug62452WithConnection(con);

    MysqlXADataSource xads = new MysqlXADataSource();
    xads.setUrl(dbUrl);

    xads.setPinGlobalTxToPhysicalConnection(false);
    con = xads.getXAConnection();
    assertTrue(con instanceof JDBC4MysqlXAConnection);
    testBug62452WithConnection(con);

    xads.setPinGlobalTxToPhysicalConnection(true);
    con = xads.getXAConnection();
    assertTrue(con instanceof JDBC4SuspendableXAConnection);
    testBug62452WithConnection(con);

}
 
Example 2
Source Project: r-course   Source File: DataSourceRegressionTest.java    License: MIT License 6 votes vote down vote up
public void testBug35810() throws Exception {
    int defaultConnectTimeout = ((ConnectionProperties) this.conn).getConnectTimeout();
    int nonDefaultConnectTimeout = defaultConnectTimeout + 1000 * 2;
    MysqlConnectionPoolDataSource cpds = new MysqlConnectionPoolDataSource();
    String dsUrl = BaseTestCase.dbUrl;
    if (dsUrl.indexOf("?") == -1) {
        dsUrl += "?";
    } else {
        dsUrl += "&";
    }

    dsUrl += "connectTimeout=" + nonDefaultConnectTimeout;
    cpds.setUrl(dsUrl);

    Connection dsConn = cpds.getPooledConnection().getConnection();
    int configuredConnectTimeout = ((ConnectionProperties) dsConn).getConnectTimeout();

    assertEquals("Connect timeout spec'd by URL didn't take", nonDefaultConnectTimeout, configuredConnectTimeout);
    assertFalse("Connect timeout spec'd by URL didn't take", defaultConnectTimeout == configuredConnectTimeout);
}
 
Example 3
/**
 * Tests fix for BUG#62452 - NPE thrown in JDBC4MySQLPooledException when statement is closed.
 * 
 * @throws Exception
 */
public void testBug62452() throws Exception {
    PooledConnection con = null;

    MysqlConnectionPoolDataSource pds = new MysqlConnectionPoolDataSource();
    pds.setUrl(dbUrl);
    con = pds.getPooledConnection();
    assertTrue(con instanceof JDBC4MysqlPooledConnection);
    testBug62452WithConnection(con);

    MysqlXADataSource xads = new MysqlXADataSource();
    xads.setUrl(dbUrl);

    xads.setPinGlobalTxToPhysicalConnection(false);
    con = xads.getXAConnection();
    assertTrue(con instanceof JDBC4MysqlXAConnection);
    testBug62452WithConnection(con);

    xads.setPinGlobalTxToPhysicalConnection(true);
    con = xads.getXAConnection();
    assertTrue(con instanceof JDBC4SuspendableXAConnection);
    testBug62452WithConnection(con);

}
 
Example 4
public void testBug35810() throws Exception {
    int defaultConnectTimeout = ((ConnectionProperties) this.conn).getConnectTimeout();
    int nonDefaultConnectTimeout = defaultConnectTimeout + 1000 * 2;
    MysqlConnectionPoolDataSource cpds = new MysqlConnectionPoolDataSource();
    String dsUrl = BaseTestCase.dbUrl;
    if (dsUrl.indexOf("?") == -1) {
        dsUrl += "?";
    } else {
        dsUrl += "&";
    }

    dsUrl += "connectTimeout=" + nonDefaultConnectTimeout;
    cpds.setUrl(dsUrl);

    Connection dsConn = cpds.getPooledConnection().getConnection();
    int configuredConnectTimeout = ((ConnectionProperties) dsConn).getConnectTimeout();

    assertEquals("Connect timeout spec'd by URL didn't take", nonDefaultConnectTimeout, configuredConnectTimeout);
    assertFalse("Connect timeout spec'd by URL didn't take", defaultConnectTimeout == configuredConnectTimeout);
}
 
Example 5
Source Project: SpinalTap   Source File: MysqlClient.java    License: Apache License 2.0 5 votes vote down vote up
public static MysqlDataSource createMysqlDataSource(
    String host,
    int port,
    String user,
    String password,
    boolean mTlsEnabled,
    TlsConfiguration tlsConfig) {
  MysqlDataSource dataSource = new MysqlConnectionPoolDataSource();

  dataSource.setUser(user);
  dataSource.setPassword(password);
  dataSource.setServerName(host);
  dataSource.setPort(port);
  dataSource.setJdbcCompliantTruncation(false);
  dataSource.setAutoReconnectForConnectionPools(true);

  if (mTlsEnabled && tlsConfig != null) {
    dataSource.setUseSSL(true);
    if (tlsConfig.getKeyStoreFilePath() != null && tlsConfig.getKeyStorePassword() != null) {
      dataSource.setClientCertificateKeyStoreUrl("file:" + tlsConfig.getKeyStoreFilePath());
      dataSource.setClientCertificateKeyStorePassword(tlsConfig.getKeyStorePassword());
    }
    if (tlsConfig.getKeyStoreType() != null) {
      dataSource.setClientCertificateKeyStoreType(tlsConfig.getKeyStoreType());
    }
    if (tlsConfig.getTrustStoreFilePath() != null && tlsConfig.getTrustStorePassword() != null) {
      dataSource.setTrustCertificateKeyStoreUrl("file:" + tlsConfig.getTrustStoreFilePath());
      dataSource.setTrustCertificateKeyStorePassword(tlsConfig.getTrustStorePassword());
    }
    if (tlsConfig.getTrustStoreType() != null) {
      dataSource.setTrustCertificateKeyStoreType(tlsConfig.getTrustStoreType());
    }
  }

  return dataSource;
}
 
Example 6
Source Project: ats-framework   Source File: DbConnMySQL.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public DataSource getDataSource() {

    MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();// do not use connection pool
    dataSource.setServerName(this.host);
    dataSource.setPort(this.port);
    dataSource.setDatabaseName(this.db);
    dataSource.setUser(this.user);
    dataSource.setPassword(this.password);
    dataSource.setAllowMultiQueries(true);

    return dataSource;
}
 
Example 7
Source Project: r-course   Source File: PooledConnectionRegressionTest.java    License: MIT License 5 votes vote down vote up
/**
 * Set up test case before a test is run.
 * 
 * @throws Exception
 */
@Override
public void setUp() throws Exception {
    super.setUp();

    // Reset event count.
    this.closeEventCount = 0;
    this.connectionErrorEventCount = 0;

    MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();

    ds.setURL(BaseTestCase.dbUrl);

    this.cpds = ds;
}
 
Example 8
Source Project: r-course   Source File: DataSourceRegressionTest.java    License: MIT License 5 votes vote down vote up
/**
 * Tests fix for BUG#4808- Calling .close() twice on a PooledConnection
 * causes NPE.
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testBug4808() throws Exception {
    MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
    ds.setURL(BaseTestCase.dbUrl);
    PooledConnection closeMeTwice = ds.getPooledConnection();
    closeMeTwice.close();
    closeMeTwice.close();

}
 
Example 9
Source Project: r-course   Source File: DataSourceRegressionTest.java    License: MIT License 5 votes vote down vote up
/**
 * Tests fix for BUG#32101 - When using a connection from our ConnectionPoolDataSource,
 * some Connection.prepareStatement() methods would return null instead of
 * a prepared statement.
 * 
 * @throws Exception
 */
public void testBug32101() throws Exception {
    MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
    ds.setURL(BaseTestCase.dbUrl);
    PooledConnection pc = ds.getPooledConnection();
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1"));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", Statement.RETURN_GENERATED_KEYS));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new int[0]));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new String[0]));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
    assertNotNull(
            pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT));
}
 
Example 10
public static void setUpDatabaseTestJNDI() throws Exception {
	DAOConfig.setHibernateConfigurationFileFile(new File("../knowage/src/hibernate.cfg.xml"));

	try {
		Class.forName("org.hsqldb.jdbcDriver");
	} catch (Exception e) {
		System.err.println("ERROR: failed to load HSQLDB JDBC driver.");
		e.printStackTrace();
		return;
	}

	// Create initial context
	System.setProperty(Context.INITIAL_CONTEXT_FACTORY, MockFactory.class.getName());

	/* Construct DataSources*/MysqlConnectionPoolDataSource knowageDs = new MysqlConnectionPoolDataSource();
	knowageDs.setURL("jdbc:mysql://localhost/knowage_master");
	knowageDs.setUser("root");
	knowageDs.setPassword("123456");

	MysqlConnectionPoolDataSource foodmartDs = new MysqlConnectionPoolDataSource();
	foodmartDs.setURL("jdbc:mysql://localhost/foodmart");
	foodmartDs.setUser("root");
	foodmartDs.setPassword("123456");

	Context ic = new MockContext();
	MockFactory.context = ic;
	ic.bind("java:/comp/env/jdbc/knowage", knowageDs);
	ic.bind("java:comp/env/jdbc/knowage", knowageDs);
	ic.bind("java:comp/env/jdbc/foodmart", foodmartDs);
}
 
Example 11
/**
 * Set up test case before a test is run.
 * 
 * @throws Exception
 */
@Override
public void setUp() throws Exception {
    super.setUp();

    // Reset event count.
    this.closeEventCount = 0;
    this.connectionErrorEventCount = 0;

    MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();

    ds.setURL(BaseTestCase.dbUrl);

    this.cpds = ds;
}
 
Example 12
/**
 * Tests fix for BUG#4808- Calling .close() twice on a PooledConnection
 * causes NPE.
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testBug4808() throws Exception {
    MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
    ds.setURL(BaseTestCase.dbUrl);
    PooledConnection closeMeTwice = ds.getPooledConnection();
    closeMeTwice.close();
    closeMeTwice.close();

}
 
Example 13
/**
 * Tests fix for BUG#32101 - When using a connection from our ConnectionPoolDataSource,
 * some Connection.prepareStatement() methods would return null instead of
 * a prepared statement.
 * 
 * @throws Exception
 */
public void testBug32101() throws Exception {
    MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
    ds.setURL(BaseTestCase.dbUrl);
    PooledConnection pc = ds.getPooledConnection();
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1"));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", Statement.RETURN_GENERATED_KEYS));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new int[0]));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", new String[0]));
    assertNotNull(pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
    assertNotNull(
            pc.getConnection().prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT));
}
 
Example 14
Source Project: r-course   Source File: ConnectionTest.java    License: MIT License 4 votes vote down vote up
public void testInterfaceImplementation() throws Exception {
    testInterfaceImplementation(getConnectionWithProps((Properties) null));
    MysqlConnectionPoolDataSource cpds = new MysqlConnectionPoolDataSource();
    cpds.setUrl(dbUrl);
    testInterfaceImplementation(cpds.getPooledConnection().getConnection());
}
 
Example 15
Source Project: r-course   Source File: DataSourceTest.java    License: MIT License 4 votes vote down vote up
/**
 * Tests whether Connection.changeUser() (and thus pooled connections)
 * restore character set information correctly.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testChangeUserAndCharsets() throws Exception {
    if (versionMeetsMinimum(4, 1)) {
        MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setURL(BaseTestCase.dbUrl);
        ds.setCharacterEncoding("utf-8");
        PooledConnection pooledConnection = ds.getPooledConnection();

        Connection connToMySQL = pooledConnection.getConnection();
        this.rs = connToMySQL.createStatement().executeQuery("SELECT @@character_set_results");
        assertTrue(this.rs.next());

        String toCheck = null;

        if (versionMeetsMinimum(4, 1, 15)) {
            if (versionMeetsMinimum(5, 0)) {
                if (versionMeetsMinimum(5, 0, 13)) {
                    toCheck = null;
                } else {
                    toCheck = "NULL";
                }
            } else {
                toCheck = null;
            }
        } else {
            toCheck = "NULL";
        }

        assertEquals(toCheck, this.rs.getString(1));

        this.rs = connToMySQL.createStatement().executeQuery("SHOW SESSION VARIABLES LIKE 'character_set_client'");
        assertTrue(this.rs.next());

        //Cause of utf8mb4
        assertEquals(0, this.rs.getString(2).indexOf("utf8"));

        connToMySQL.close();

        connToMySQL = pooledConnection.getConnection();
        this.rs = connToMySQL.createStatement().executeQuery("SELECT @@character_set_results");
        assertTrue(this.rs.next());
        assertEquals(toCheck, this.rs.getString(1));

        this.rs = connToMySQL.createStatement().executeQuery("SHOW SESSION VARIABLES LIKE 'character_set_client'");
        assertTrue(this.rs.next());

        //Cause of utf8mb4
        assertEquals(0, this.rs.getString(2).indexOf("utf8"));

        pooledConnection.getConnection().close();
    }
}
 
Example 16
Source Project: Komondor   Source File: ConnectionTest.java    License: GNU General Public License v3.0 4 votes vote down vote up
public void testInterfaceImplementation() throws Exception {
    testInterfaceImplementation(getConnectionWithProps((Properties) null));
    MysqlConnectionPoolDataSource cpds = new MysqlConnectionPoolDataSource();
    cpds.setUrl(dbUrl);
    testInterfaceImplementation(cpds.getPooledConnection().getConnection());
}
 
Example 17
Source Project: Komondor   Source File: DataSourceTest.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests whether Connection.changeUser() (and thus pooled connections)
 * restore character set information correctly.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testChangeUserAndCharsets() throws Exception {
    if (versionMeetsMinimum(4, 1)) {
        MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setURL(BaseTestCase.dbUrl);
        ds.setCharacterEncoding("utf-8");
        PooledConnection pooledConnection = ds.getPooledConnection();

        Connection connToMySQL = pooledConnection.getConnection();
        this.rs = connToMySQL.createStatement().executeQuery("SELECT @@character_set_results");
        assertTrue(this.rs.next());

        String toCheck = null;

        if (versionMeetsMinimum(4, 1, 15)) {
            if (versionMeetsMinimum(5, 0)) {
                if (versionMeetsMinimum(5, 0, 13)) {
                    toCheck = null;
                } else {
                    toCheck = "NULL";
                }
            } else {
                toCheck = null;
            }
        } else {
            toCheck = "NULL";
        }

        assertEquals(toCheck, this.rs.getString(1));

        this.rs = connToMySQL.createStatement().executeQuery("SHOW SESSION VARIABLES LIKE 'character_set_client'");
        assertTrue(this.rs.next());

        //Cause of utf8mb4
        assertEquals(0, this.rs.getString(2).indexOf("utf8"));

        connToMySQL.close();

        connToMySQL = pooledConnection.getConnection();
        this.rs = connToMySQL.createStatement().executeQuery("SELECT @@character_set_results");
        assertTrue(this.rs.next());
        assertEquals(toCheck, this.rs.getString(1));

        this.rs = connToMySQL.createStatement().executeQuery("SHOW SESSION VARIABLES LIKE 'character_set_client'");
        assertTrue(this.rs.next());

        //Cause of utf8mb4
        assertEquals(0, this.rs.getString(2).indexOf("utf8"));

        pooledConnection.getConnection().close();
    }
}
 
Example 18
Source Project: ats-framework   Source File: Test_DbConnMySQL.java    License: Apache License 2.0 3 votes vote down vote up
@Test
public void getDataSource() {

    DbConnMySQL dbConnection = new DbConnMySQL("host", "db", "user", "pass");

    assertEquals(MysqlConnectionPoolDataSource.class, dbConnection.getDataSource().getClass());
}