当前位置: 首页>>代码示例>>Java>>正文


Java PooledDataSource.getConnection方法代码示例

本文整理汇总了Java中org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java PooledDataSource.getConnection方法的具体用法?Java PooledDataSource.getConnection怎么用?Java PooledDataSource.getConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.ibatis.datasource.pooled.PooledDataSource的用法示例。


在下文中一共展示了PooledDataSource.getConnection方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: shouldNotFailCallingToStringOverAnInvalidConnection

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
@Test
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  Connection c = ds.getConnection();
  c.close();
  c.toString();
}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:8,代码来源:PooledDataSourceTest.java

示例2: assertProductsTableExistsAndLoaded

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
private void assertProductsTableExistsAndLoaded() throws IOException, SQLException {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  try {
    Connection conn = ds.getConnection();
    SqlRunner executor = new SqlRunner(conn);
    List<Map<String, Object>> products = executor.selectAll("SELECT * FROM PRODUCT");
    assertEquals(16, products.size());
  } finally {
    ds.forceCloseAll();
  }
}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:12,代码来源:ScriptRunnerTest.java

示例3: testPerformDatabaseSchemaOperationCreateTwice

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
public void testPerformDatabaseSchemaOperationCreateTwice() throws Exception {

    // both process engines will be using this datasource.
    PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(), "org.h2.Driver",
        "jdbc:h2:mem:DatabaseTablePrefixTest;DB_CLOSE_DELAY=1000", "sa", "");

    Connection connection = pooledDataSource.getConnection();
    connection.createStatement().execute("drop schema if exists " + SCHEMA_NAME);
    connection.createStatement().execute("create schema " + SCHEMA_NAME);
    connection.close();

    ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration().setProcessEngineName("DatabaseTablePrefixTest-engine1")
    // disable auto create/drop schema
        .setDataSource(pooledDataSource).setDatabaseSchemaUpdate("NO_CHECK");
    config1.setDatabaseTablePrefix(SCHEMA_NAME + ".");
    config1.setDatabaseSchema(SCHEMA_NAME);
    config1.setDbMetricsReporterActivate(false);
    ProcessEngine engine1 = config1.buildProcessEngine();

    // create the tables for the first time
    connection = pooledDataSource.getConnection();
    connection.createStatement().execute("set schema " + SCHEMA_NAME);
    engine1.getManagementService().databaseSchemaUpgrade(connection, "", SCHEMA_NAME);
    connection.close();
    // create the tables for the second time; here we shouldn't crash since the
    // session should tell us that the tables are already present and
    // databaseSchemaUpdate is set to noop
    connection = pooledDataSource.getConnection();
    connection.createStatement().execute("set schema " + SCHEMA_NAME);
    engine1.getManagementService().databaseSchemaUpgrade(connection, "", SCHEMA_NAME);
    engine1.close();
  }
 
开发者ID:camunda,项目名称:camunda-bpm-platform,代码行数:33,代码来源:DatabaseTableSchemaTest.java

示例4: testTablePresentWithSchemaAndPrefix

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
public void testTablePresentWithSchemaAndPrefix() throws SQLException {
  PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(), "org.h2.Driver",
      "jdbc:h2:mem:DatabaseTablePrefixTest;DB_CLOSE_DELAY=1000", "sa", "");

  Connection connection = pooledDataSource.getConnection();
  connection.createStatement().execute("drop schema if exists " + SCHEMA_NAME);
  connection.createStatement().execute("create schema " + SCHEMA_NAME);
  connection.createStatement().execute("create table " + SCHEMA_NAME + "." + PREFIX_NAME + "SOME_TABLE(id varchar(64));");
  connection.close();

  ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration().setProcessEngineName("DatabaseTablePrefixTest-engine1")
  // disable auto create/drop schema
      .setDataSource(pooledDataSource).setDatabaseSchemaUpdate("NO_CHECK");
  config1.setDatabaseTablePrefix(SCHEMA_NAME + "." + PREFIX_NAME);
  config1.setDatabaseSchema(SCHEMA_NAME);
  config1.setDbMetricsReporterActivate(false);
  ProcessEngine engine = config1.buildProcessEngine();
  CommandExecutor commandExecutor = config1.getCommandExecutorTxRequired();

  commandExecutor.execute(new Command<Void>(){
    public Void execute(CommandContext commandContext) {
      DbSqlSession sqlSession = commandContext.getSession(DbSqlSession.class);
      assertTrue(sqlSession.isTablePresent("SOME_TABLE"));
      return null;
    }
  });

  engine.close();

}
 
开发者ID:camunda,项目名称:camunda-bpm-platform,代码行数:31,代码来源:DatabaseTableSchemaTest.java

示例5: afterPropertiesSet

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
public void afterPropertiesSet() throws Exception {

    dataSource = new PooledDataSource(ReflectUtil.getClassLoader(),
            "org.h2.Driver",
            "jdbc:h2:mem:DatabaseTablePrefixTest;DB_CLOSE_DELAY=1000;MVCC=TRUE;",
            "sa",
            "" );

    // create schema in the
    Connection connection = dataSource.getConnection();
    connection.createStatement().execute("drop schema if exists SCHEMA1");
    connection.createStatement().execute("create schema SCHEMA1");
    connection.close();

    ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration()
            .setProcessEngineName("DatabaseTablePrefixTest-engine1")
            .setDataSource(dataSource)
            .setDbMetricsReporterActivate(false)
            .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
    config1.setDatabaseTablePrefix("SCHEMA1.");
    ProcessEngine engine1 = config1.buildProcessEngine();

    // create the tables in SCHEMA1
    connection = dataSource.getConnection();
    connection.createStatement().execute("set schema SCHEMA1");
    engine1.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA1");
    connection.close();

    engine1.close();

  }
 
开发者ID:camunda,项目名称:camunda-bpm-platform,代码行数:32,代码来源:DbSchemaPrefixTestHelper.java

示例6: ShouldReturnRealConnection

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
@Test
public void ShouldReturnRealConnection() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  Connection c = ds.getConnection();
  JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
  c.close();
}
 
开发者ID:mybatis,项目名称:mybatis-3,代码行数:8,代码来源:PooledDataSourceTest.java

示例7: assertProductsTableExistsAndLoaded

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
private void assertProductsTableExistsAndLoaded() throws IOException, SQLException {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  try {
    Connection conn = ds.getConnection();
    SqlRunner executor = new SqlRunner(conn);
    List<Map<String, Object>> products = executor.selectAll("SELECT * FROM PRODUCT");
    assertEquals(16, products.size());
    conn.close();
  } finally {
    ds.forceCloseAll();
  }
}
 
开发者ID:mybatis,项目名称:mybatis-3,代码行数:13,代码来源:ScriptRunnerTest.java

示例8: ShouldReturnRealConnection

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
@Test
public void ShouldReturnRealConnection() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  Connection c = ds.getConnection();
  JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:7,代码来源:PooledDataSourceTest.java

示例9: shouldReconnectWhenServerKilledLeakedConnection

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
@Ignore("See the comments")
@Test
public void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
  // See #748
  // Requirements:
  // 1. MySQL JDBC driver dependency.
  // 2. MySQL server instance with the following.
  //  - CREATE DATABASE `test`;
  //  - SET GLOBAL wait_timeout=3;
  // 3. Tweak the connection info below.
  final String URL = "jdbc:mysql://localhost:3306/test";
  final String USERNAME = "admin";
  final String PASSWORD = "";

  Connection con;
  PooledDataSource ds = new PooledDataSource();
  ds.setDriver("com.mysql.jdbc.Driver");
  ds.setUrl(URL);
  ds.setUsername(USERNAME);
  ds.setPassword(PASSWORD);
  ds.setPoolMaximumActiveConnections(1);
  ds.setPoolMaximumIdleConnections(1);
  ds.setPoolTimeToWait(1000);
  ds.setPoolMaximumCheckoutTime(2000);
  ds.setPoolPingEnabled(true);
  ds.setPoolPingQuery("select 1");
  ds.setDefaultAutoCommit(true);
  // MySQL wait_timeout * 1000 or less. (unit:ms)
  ds.setPoolPingConnectionsNotUsedFor(1000);

  con = ds.getConnection();
  exexuteQuery(con);
  // Simulate connection leak by not closing.
  // con.close();

  // Wait for disconnected from mysql...
  Thread.sleep(TimeUnit.SECONDS.toMillis(3));

  // Should return usable connection.
  con = ds.getConnection();
  exexuteQuery(con);
  con.close();
}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:44,代码来源:PooledDataSourceTest.java

示例10: testPerformDatabaseSchemaOperationCreate

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
public void testPerformDatabaseSchemaOperationCreate() throws Exception {

        // both process engines will be using this datasource.
        PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(),
                "org.h2.Driver",
                "jdbc:h2:mem:activiti-test;DB_CLOSE_DELAY=1000",
                "sa",
                "");

        // create two schemas is the database
        Connection connection = pooledDataSource.getConnection();
        connection.createStatement().execute("drop schema if exists SCHEMA1");
        connection.createStatement().execute("drop schema if exists SCHEMA2");
        connection.createStatement().execute("create schema SCHEMA1");
        connection.createStatement().execute("create schema SCHEMA2");
        connection.close();

        // configure & build two different process engines, each having a separate table prefix
        ProcessEngineConfigurationImpl config1 = (ProcessEngineConfigurationImpl) ProcessEngineConfigurationImpl
                .createStandaloneInMemProcessEngineConfiguration()
                .setDataSource(pooledDataSource)
                .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
        config1.setDatabaseTablePrefix("SCHEMA1.");
        config1.setFlowable5CompatibilityEnabled(true);
        config1.setValidateFlowable5EntitiesEnabled(false);
        config1.getPerformanceSettings().setValidateExecutionRelationshipCountConfigOnBoot(false);
        config1.getPerformanceSettings().setValidateTaskRelationshipCountConfigOnBoot(false);
        ProcessEngine engine1 = config1.buildProcessEngine();

        ProcessEngineConfigurationImpl config2 = (ProcessEngineConfigurationImpl) ProcessEngineConfigurationImpl
                .createStandaloneInMemProcessEngineConfiguration()
                .setDataSource(pooledDataSource)
                .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
        config2.setDatabaseTablePrefix("SCHEMA2.");
        config2.setFlowable5CompatibilityEnabled(true);
        config2.setValidateFlowable5EntitiesEnabled(false);
        config2.getPerformanceSettings().setValidateExecutionRelationshipCountConfigOnBoot(false);
        config2.getPerformanceSettings().setValidateTaskRelationshipCountConfigOnBoot(false);
        ProcessEngine engine2 = config2.buildProcessEngine();

        // create the tables in SCHEMA1
        connection = pooledDataSource.getConnection();
        connection.createStatement().execute("set schema SCHEMA1");
        engine1.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA1");
        connection.close();

        // create the tables in SCHEMA2
        connection = pooledDataSource.getConnection();
        connection.createStatement().execute("set schema SCHEMA2");
        engine2.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA2");
        connection.close();

        // if I deploy a process to one engine, it is not visible to the other engine:
        try {
            engine1.getRepositoryService()
                    .createDeployment()
                    .addClasspathResource("org/activiti/engine/test/db/oneJobProcess.bpmn20.xml")
                    .deploymentProperty(DeploymentProperties.DEPLOY_AS_FLOWABLE5_PROCESS_DEFINITION, Boolean.TRUE)
                    .deploy();

            assertEquals(1, engine1.getRepositoryService().createDeploymentQuery().count());
            assertEquals(0, engine2.getRepositoryService().createDeploymentQuery().count());

        } finally {
            engine1.close();
            engine2.close();
        }
    }
 
开发者ID:flowable,项目名称:flowable-engine,代码行数:69,代码来源:DatabaseTablePrefixTest.java

示例11: testPerformDatabaseSchemaOperationCreate

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
public void testPerformDatabaseSchemaOperationCreate() throws Exception {

        // both process engines will be using this datasource.
        PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(), "org.h2.Driver", "jdbc:h2:mem:activiti-test;DB_CLOSE_DELAY=1000", "sa", "");

        // create two schemas is the database
        Connection connection = pooledDataSource.getConnection();
        connection.createStatement().execute("drop schema if exists SCHEMA1");
        connection.createStatement().execute("drop schema if exists SCHEMA2");
        connection.createStatement().execute("create schema SCHEMA1");
        connection.createStatement().execute("create schema SCHEMA2");
        connection.close();

        // configure & build two different process engines, each having a
        // separate table prefix
        ProcessEngineConfigurationImpl config1 = (ProcessEngineConfigurationImpl) ProcessEngineConfigurationImpl.createStandaloneInMemProcessEngineConfiguration().setDataSource(pooledDataSource)
                .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
        config1.setDatabaseTablePrefix("SCHEMA1.");
        config1.setValidateFlowable5EntitiesEnabled(false);
        config1.getPerformanceSettings().setValidateExecutionRelationshipCountConfigOnBoot(false);
        config1.getPerformanceSettings().setValidateTaskRelationshipCountConfigOnBoot(false);
        ProcessEngine engine1 = config1.buildProcessEngine();

        ProcessEngineConfigurationImpl config2 = (ProcessEngineConfigurationImpl) ProcessEngineConfigurationImpl.createStandaloneInMemProcessEngineConfiguration().setDataSource(pooledDataSource)
                .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
        config2.setDatabaseTablePrefix("SCHEMA2.");
        config2.setValidateFlowable5EntitiesEnabled(false);
        config2.getPerformanceSettings().setValidateExecutionRelationshipCountConfigOnBoot(false);
        config2.getPerformanceSettings().setValidateTaskRelationshipCountConfigOnBoot(false);
        ProcessEngine engine2 = config2.buildProcessEngine();

        // create the tables in SCHEMA1
        connection = pooledDataSource.getConnection();
        connection.createStatement().execute("set schema SCHEMA1");
        engine1.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA1");
        connection.close();

        // create the tables in SCHEMA2
        connection = pooledDataSource.getConnection();
        connection.createStatement().execute("set schema SCHEMA2");
        engine2.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA2");
        connection.close();

        // if I deploy a process to one engine, it is not visible to the other
        // engine:
        try {
            engine1.getRepositoryService().createDeployment().addClasspathResource("org/flowable/engine/test/db/oneJobProcess.bpmn20.xml").deploy();

            assertEquals(1, engine1.getRepositoryService().createDeploymentQuery().count());
            assertEquals(0, engine2.getRepositoryService().createDeploymentQuery().count());

        } finally {
            engine1.close();
            engine2.close();
        }
    }
 
开发者ID:flowable,项目名称:flowable-engine,代码行数:57,代码来源:DatabaseTablePrefixTest.java

示例12: testPerformDatabaseSchemaOperationCreate

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
public void testPerformDatabaseSchemaOperationCreate() throws Exception{
  
  // both process engines will be using this datasource.
  PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(), 
          "org.h2.Driver", 
          "jdbc:h2:mem:activiti-test;DB_CLOSE_DELAY=1000", 
          "sa", 
          "" );
        
  // create two schemas is the database
  Connection connection = pooledDataSource.getConnection();
  connection.createStatement().execute("drop schema if exists SCHEMA1");
  connection.createStatement().execute("drop schema if exists SCHEMA2");
  connection.createStatement().execute("create schema SCHEMA1");
  connection.createStatement().execute("create schema SCHEMA2");    
  connection.close();

  // configure & build two different process engines, each having a separate table prefix 
  ProcessEngineConfigurationImpl config1 = (ProcessEngineConfigurationImpl) ProcessEngineConfigurationImpl
          .createStandaloneInMemProcessEngineConfiguration()
          .setDataSource(pooledDataSource)
          .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
  config1.setDatabaseTablePrefix("SCHEMA1.");
  ProcessEngine engine1 = config1.buildProcessEngine();
  
  ProcessEngineConfigurationImpl config2 = (ProcessEngineConfigurationImpl) ProcessEngineConfigurationImpl
          .createStandaloneInMemProcessEngineConfiguration()
          .setDataSource(pooledDataSource)
          .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema        
  config2.setDatabaseTablePrefix("SCHEMA2.");
  ProcessEngine engine2 = config2.buildProcessEngine();
  
  // create the tables in SCHEMA1
  connection = pooledDataSource.getConnection();
  connection.createStatement().execute("set schema SCHEMA1");    
  engine1.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA1");
  connection.close();

  // create the tables in SCHEMA2
  connection = pooledDataSource.getConnection();
  connection.createStatement().execute("set schema SCHEMA2");    
  engine2.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA2");
  connection.close();

  // if I deploy a process to one engine, it is not visible to the other
  // engine:
  try {
    engine1.getRepositoryService()
      .createDeployment()
      .addClasspathResource("org/activiti/engine/test/db/oneJobProcess.bpmn20.xml")
      .deploy();

    assertEquals(1, engine1.getRepositoryService().createDeploymentQuery().count());
    assertEquals(0, engine2.getRepositoryService().createDeploymentQuery().count());

  } finally {
    engine1.close();
    engine2.close();
  }
}
 
开发者ID:springvelocity,项目名称:xbpm5,代码行数:61,代码来源:DatabaseTablePrefixTest.java

示例13: shouldPerformDatabaseSchemaOperationCreate

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
@Test
public void shouldPerformDatabaseSchemaOperationCreate() throws Exception{

  // both process engines will be using this datasource.
  PooledDataSource pooledDataSource = new PooledDataSource(ReflectUtil.getClassLoader(),
          "org.h2.Driver",
          "jdbc:h2:mem:DatabaseTablePrefixTest;DB_CLOSE_DELAY=1000",
          "sa",
          "" );

  // create two schemas is the database
  Connection connection = pooledDataSource.getConnection();
  connection.createStatement().execute("drop schema if exists SCHEMA1");
  connection.createStatement().execute("drop schema if exists SCHEMA2");
  connection.createStatement().execute("create schema SCHEMA1");
  connection.createStatement().execute("create schema SCHEMA2");
  connection.close();

  // configure & build two different process engines, each having a separate table prefix
  ProcessEngineConfigurationImpl config1 = createCustomProcessEngineConfiguration()
          .setProcessEngineName("DatabaseTablePrefixTest-engine1")
          .setDataSource(pooledDataSource)
          .setDbMetricsReporterActivate(false)
          .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
  config1.setDatabaseTablePrefix("SCHEMA1.");
  config1.setUseSharedSqlSessionFactory(true);
  ProcessEngine engine1 = config1.buildProcessEngine();

  ProcessEngineConfigurationImpl config2 = createCustomProcessEngineConfiguration()
          .setProcessEngineName("DatabaseTablePrefixTest-engine2")
          .setDataSource(pooledDataSource)
          .setDbMetricsReporterActivate(false)
          .setDatabaseSchemaUpdate("NO_CHECK"); // disable auto create/drop schema
  config2.setDatabaseTablePrefix("SCHEMA2.");
  config2.setUseSharedSqlSessionFactory(true);
  ProcessEngine engine2 = config2.buildProcessEngine();

  // create the tables in SCHEMA1
  connection = pooledDataSource.getConnection();
  connection.createStatement().execute("set schema SCHEMA1");
  engine1.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA1");
  connection.close();

  // create the tables in SCHEMA2
  connection = pooledDataSource.getConnection();
  connection.createStatement().execute("set schema SCHEMA2");
  engine2.getManagementService().databaseSchemaUpgrade(connection, "", "SCHEMA2");
  connection.close();

  // if I deploy a process to one engine, it is not visible to the other
  // engine:
  try {
    engine1.getRepositoryService()
      .createDeployment()
      .addClasspathResource("org/camunda/bpm/engine/test/api/cfg/oneJobProcess.bpmn20.xml")
      .deploy();

    assertEquals(1, engine1.getRepositoryService().createDeploymentQuery().count());
    assertEquals(0, engine2.getRepositoryService().createDeploymentQuery().count());

  } finally {
    engine1.close();
    engine2.close();
    ProcessEngineConfigurationImpl.cachedSqlSessionFactory = null;
  }
}
 
开发者ID:camunda,项目名称:camunda-bpm-platform,代码行数:67,代码来源:DatabaseTablePrefixTest.java

示例14: shouldReconnectWhenServerKilledLeakedConnection

import org.apache.ibatis.datasource.pooled.PooledDataSource; //导入方法依赖的package包/类
@Ignore("See the comments")
@Test
public void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
  // See #748
  // Requirements:
  // 1. MySQL JDBC driver dependency.
  // 2. MySQL server instance with the following.
  //  - CREATE DATABASE `test`;
  //  - SET GLOBAL wait_timeout=3;
  // 3. Tweak the connection info below.
  final String URL = "jdbc:mysql://localhost:3306/test";
  final String USERNAME = "admin";
  final String PASSWORD = "";

  PooledDataSource ds = new PooledDataSource();
  ds.setDriver("com.mysql.jdbc.Driver");
  ds.setUrl(URL);
  ds.setUsername(USERNAME);
  ds.setPassword(PASSWORD);
  ds.setPoolMaximumActiveConnections(1);
  ds.setPoolMaximumIdleConnections(1);
  ds.setPoolTimeToWait(1000);
  ds.setPoolMaximumCheckoutTime(2000);
  ds.setPoolPingEnabled(true);
  ds.setPoolPingQuery("select 1");
  ds.setDefaultAutoCommit(true);
  // MySQL wait_timeout * 1000 or less. (unit:ms)
  ds.setPoolPingConnectionsNotUsedFor(1000);

  Connection con = ds.getConnection();
  exexuteQuery(con);
  // Simulate connection leak by not closing.
  // con.close();

  // Wait for disconnected from mysql...
  Thread.sleep(TimeUnit.SECONDS.toMillis(3));

  con.close();

  // Should return usable connection.
  con = ds.getConnection();
  exexuteQuery(con);
  con.close();
}
 
开发者ID:mybatis,项目名称:mybatis-3,代码行数:45,代码来源:PooledDataSourceTest.java


注:本文中的org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。