Search in sources :

Example 66 with Connection

use of java.sql.Connection in project HikariCP by brettwooldridge.

the class TestHibernate method testConnectionProvider.

@Test
public void testConnectionProvider() throws Exception {
    HikariConnectionProvider provider = new HikariConnectionProvider();
    Properties props = new Properties();
    props.load(getClass().getResourceAsStream("/hibernate.properties"));
    provider.configure(props);
    Connection connection = provider.getConnection();
    provider.closeConnection(connection);
    assertNotNull(provider.unwrap(HikariConnectionProvider.class));
    assertFalse(provider.supportsAggressiveRelease());
    try {
        provider.unwrap(TestHibernate.class);
        fail("Expected exception");
    } catch (UnknownUnwrapTypeException e) {
    }
    provider.stop();
}
Also used : Connection(java.sql.Connection) HikariConnectionProvider(com.zaxxer.hikari.hibernate.HikariConnectionProvider) Properties(java.util.Properties) UnknownUnwrapTypeException(org.hibernate.service.UnknownUnwrapTypeException) Test(org.junit.Test)

Example 67 with Connection

use of java.sql.Connection in project HikariCP by brettwooldridge.

the class TestMetrics method testSetters1.

@Test
public void testSetters1() throws Exception {
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMaximumPoolSize(1);
        ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        MetricRegistry metricRegistry = new MetricRegistry();
        HealthCheckRegistry healthRegistry = new HealthCheckRegistry();
        try {
            try (Connection connection = ds.getConnection()) {
            // close immediately
            }
            // After the pool as started, we can only set them once...
            ds.setMetricRegistry(metricRegistry);
            ds.setHealthCheckRegistry(healthRegistry);
            // and never again...
            ds.setMetricRegistry(metricRegistry);
            fail("Should not have been allowed to set registry after pool started");
        } catch (IllegalStateException ise) {
            // pass
            try {
                ds.setHealthCheckRegistry(healthRegistry);
                fail("Should not have been allowed to set registry after pool started");
            } catch (IllegalStateException ise2) {
            // pass
            }
        }
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) MetricRegistry(com.codahale.metrics.MetricRegistry) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) Connection(java.sql.Connection) Test(org.junit.Test)

Example 68 with Connection

use of java.sql.Connection in project eweb4j-framework by laiweiwei.

the class UpdateDAOImpl method updateByFields.

public <T> Number[] updateByFields(T[] ts, String... fields) throws DAOException {
    Number[] ids = null;
    if (ts != null && ts.length > 0 && fields != null && fields.length > 0) {
        Connection con = null;
        ids = new Number[ts.length];
        try {
            Sql[] sqls = SqlFactory.getUpdateSql(ts).update(fields);
            for (int i = 0; i < ts.length; i++) {
                con = ds.getConnection();
                ids[i] = JdbcUtil.updateWithArgs(con, sqls[i].sql, sqls[i].args.toArray());
            // 更新缓存
            }
        } catch (Exception e) {
            throw new DAOException("updateByFields exception ", e);
        }
    }
    return ids;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException) Sql(org.eweb4j.orm.sql.Sql)

Example 69 with Connection

use of java.sql.Connection in project eweb4j-framework by laiweiwei.

the class UpdateDAOImpl method batchUpdate.

public <T> Number[] batchUpdate(T[] ts, String[] fields, Object[] values) throws DAOException {
    Number[] ids = null;
    if (ts != null && ts.length > 0) {
        Connection con = null;
        ids = new Number[ts.length];
        try {
            con = ds.getConnection();
            Sql[] sqls = SqlFactory.getUpdateSql(ts).update(fields, values);
            List<Object[]> argList = new ArrayList<Object[]>(ts.length);
            for (Sql sql : sqls) {
                argList.add(sql.args.toArray());
            }
            Object[][] args = new Object[argList.size()][];
            for (int i = 0; i < argList.size(); i++) {
                args[i] = argList.get(i);
            }
            ids = JdbcUtil.batchUpdateWithArgs(con, sqls[0].sql, args);
        } catch (Exception e) {
            throw new DAOException("batchUpdate exception ", e);
        }
    }
    return ids;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DAOException(org.eweb4j.orm.dao.DAOException) Sql(org.eweb4j.orm.sql.Sql)

Example 70 with Connection

use of java.sql.Connection in project eweb4j-framework by laiweiwei.

the class DataSourceWrap method getConnection.

/**
	 * 
	 */
public Connection getConnection() {
    try {
        Connection con = null;
        // 若没有开启,则正常的从数据源获取一条连接
        if (!ConThreadLocal.isTrans())
            // 这个是真正的数据源取出来的连接对象
            return ds.getConnection();
        // 若开启,则从当前线程连接池中获取数据库连接
        // 这样能保证当前线程下任何地方获取的数据库连接都是唯一的
        con = ConThreadLocal.getCon(dsName);
        if (con == null) {
            // 如果没有,就从连接池取出来一条
            con = ds.getConnection();
            con.setAutoCommit(false);
            // 然后放入到本地线程变量中保存
            ConThreadLocal.put(dsName, con);
        }
        return con;
    } catch (SQLException e) {
        log.error(e.toString(), e);
    // try {
    // this.finalize();
    // } catch (Throwable e1) {
    // e1.printStackTrace();
    // }
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Aggregations

Connection (java.sql.Connection)7652 PreparedStatement (java.sql.PreparedStatement)3358 SQLException (java.sql.SQLException)3242 ResultSet (java.sql.ResultSet)3236 Test (org.junit.Test)2507 Statement (java.sql.Statement)1631 Properties (java.util.Properties)1202 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)689 ArrayList (java.util.ArrayList)631 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)232 IOException (java.io.IOException)227 DataSource (javax.sql.DataSource)223 BaseTest (org.apache.phoenix.query.BaseTest)201 CallableStatement (java.sql.CallableStatement)194 DatabaseMetaData (java.sql.DatabaseMetaData)174 HashMap (java.util.HashMap)164 Reader (java.io.Reader)145 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)143 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)134 Map (java.util.Map)126