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();
}
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
}
}
}
}
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;
}
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;
}
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;
}
Aggregations