本文整理汇总了Java中javax.sql.PooledConnection.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java PooledConnection.getConnection方法的具体用法?Java PooledConnection.getConnection怎么用?Java PooledConnection.getConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sql.PooledConnection
的用法示例。
在下文中一共展示了PooledConnection.getConnection方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: queryTable
import javax.sql.PooledConnection; //导入方法依赖的package包/类
@TestFactory
public Stream<DynamicTest> queryTable() throws SQLException {
List<ChangeKey> changeCaptureTables = new ArrayList<>();
PooledConnection pooledConnection = null;
try {
pooledConnection = JdbcUtils.openPooledConnection(this.config, new ChangeKey(MsSqlTestConstants.DATABASE_NAME, null, null));
MsSqlQueryBuilder queryBuilder = new MsSqlQueryBuilder(pooledConnection.getConnection());
try (PreparedStatement statement = queryBuilder.listChangeTrackingTablesStatement()) {
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
String databaseName = resultSet.getString("databaseName");
String schemaName = resultSet.getString("schemaName");
String tableName = resultSet.getString("tableName");
ChangeKey changeKey = new ChangeKey(databaseName, schemaName, tableName);
changeCaptureTables.add(changeKey);
log.trace("Found Change Tracking Enabled Table {}", changeKey);
}
}
}
} finally {
JdbcUtils.closeConnection(pooledConnection);
}
return changeCaptureTables.stream().map(data -> dynamicTest(data.tableName, () -> queryTable(data)));
}
示例2: getConnection3
import javax.sql.PooledConnection; //导入方法依赖的package包/类
private synchronized Connection getConnection3() throws SQLException {
if (isDisposed) { // test again within synchronized lock
throw new IllegalStateException("Connection pool has been disposed.");
}
PooledConnection pconn;
if (!recycledConnections.isEmpty()) {
pconn = recycledConnections.remove();
} else {
pconn = dataSource.getPooledConnection();
pconn.addConnectionEventListener(poolConnectionEventListener);
}
Connection conn;
try {
// The JDBC driver may call ConnectionEventListener.connectionErrorOccurred()
// from within PooledConnection.getConnection(). To detect this within
// disposeConnection(), we temporarily set connectionInTransition.
connectionInTransition = pconn;
conn = pconn.getConnection();
} finally {
connectionInTransition = null;
}
activeConnections++;
assertInnerState();
return conn;
}
示例3: testValidateConnection
import javax.sql.PooledConnection; //导入方法依赖的package包/类
/**
* Test of validateConnection method, of class
* org.apache.geode.internal.datasource.AbstractPoolCache.
*/
@Test
public void testValidateConnection() throws Exception {
Context ctx = cache.getJNDIContext();
GemFireConnPooledDataSource ds =
(GemFireConnPooledDataSource) ctx.lookup("java:/PooledDataSource");
GemFireConnectionPoolManager provider =
(GemFireConnectionPoolManager) ds.getConnectionProvider();
ConnectionPoolCacheImpl poolCache = (ConnectionPoolCacheImpl) provider.getConnectionPoolCache();
PooledConnection poolConn = (PooledConnection) poolCache.getPooledConnectionFromPool();
Connection conn = poolConn.getConnection();
if (!ds.validateConnection(conn))
fail("validate connection failed");
conn.close();
if (ds.validateConnection(conn))
fail("validate connection failed");
}
示例4: testPacketTooLargeException
import javax.sql.PooledConnection; //导入方法依赖的package包/类
/**
* Tests that PacketTooLargeException doesn't clober the connection.
*
* @throws Exception
* if the test fails.
*/
public void testPacketTooLargeException() throws Exception {
final ConnectionEventListener conListener = new ConnectionListener();
PooledConnection pc = null;
pc = this.cpds.getPooledConnection();
pc.addConnectionEventListener(conListener);
createTable("testPacketTooLarge", "(field1 LONGBLOB)");
Connection connFromPool = pc.getConnection();
PreparedStatement pstmtFromPool = ((ConnectionWrapper) connFromPool).clientPrepare("INSERT INTO testPacketTooLarge VALUES (?)");
this.rs = this.stmt.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet'");
this.rs.next();
int maxAllowedPacket = this.rs.getInt(2);
int numChars = (int) (maxAllowedPacket * 1.2);
pstmtFromPool.setBinaryStream(1, new BufferedInputStream(new FileInputStream(newTempBinaryFile("testPacketTooLargeException", numChars))), numChars);
try {
pstmtFromPool.executeUpdate();
fail("Expecting PacketTooLargeException");
} catch (PacketTooBigException ptbe) {
// We're expecting this one...
}
// This should still work okay, even though the last query on the same connection didn't...
this.rs = connFromPool.createStatement().executeQuery("SELECT 1");
assertTrue(this.connectionErrorEventCount == 0);
assertTrue(this.closeEventCount == 0);
}
示例5: getSQLConnection
import javax.sql.PooledConnection; //导入方法依赖的package包/类
/**
* gets the connection from the pool
*
* @param poolC
* @return ???
*/
protected Connection getSQLConnection(PooledConnection poolC) throws SQLException {
Connection conn = poolC.getConnection();
boolean val = validateConnection(conn);
if (val)
return conn;
else {
provider.returnAndExpireConnection(poolC);
throw new SQLException(
LocalizedStrings.GemFireTransactionDataSource_GEMFIRECONNPOOLEDDATASOURCEGETCONNFROMCONNPOOLJAVASQLCONNECTION_OBTAINED_IS_INVALID
.toLocalizedString());
}
}
示例6: getSQLConnection
import javax.sql.PooledConnection; //导入方法依赖的package包/类
/**
* gets tha connection from the pool
*
* @param poolC
* @return ???
*/
protected Connection getSQLConnection(PooledConnection poolC) throws SQLException {
Connection conn = poolC.getConnection();
boolean val = validateConnection(conn);
if (val)
return conn;
else {
provider.returnAndExpireConnection(poolC);
throw new SQLException(
LocalizedStrings.GemFireConnPooledDataSource_GEMFIRECONNPOOLEDDATASOURCEGETCONNFROMCONNPOOLJAVASQLCONNECTION_OBTAINED_IS_INVALID
.toLocalizedString());
}
}
示例7: createDataSourceTest
import javax.sql.PooledConnection; //导入方法依赖的package包/类
@Test
public void createDataSourceTest() throws SQLException
{
CloudSpannerConnectionPoolDataSource subject = new CloudSpannerConnectionPoolDataSource();
subject.setProjectId("helpful-adroit-123456");
subject.setInstanceId("test-instance");
subject.setDatabase("test");
subject.setPvtKeyPath("C:\\Users\\MyUserName\\Documents\\CloudSpannerKeys\\cloudspanner-key.json");
subject.setPvtKeyPath(null);
subject.setOauthAccessToken("TEST");
subject.setSimulateProductName("PostgreSQL");
subject.setAllowExtendedMode(true);
subject.setLoginTimeout(10);
subject.setDefaultAutoCommit(false);
subject.setLogWriter(new PrintWriter(System.out));
Assert.assertEquals("ConnectionPoolDataSource from " + nl.topicus.jdbc.CloudSpannerDriver.getVersion(),
subject.getDescription());
PooledConnection con = subject.getPooledConnection();
Assert.assertNotNull(con);
ICloudSpannerConnection connection = (ICloudSpannerConnection) con.getConnection();
Assert.assertEquals("jdbc:cloudspanner://localhost", connection.getUrl());
Assert.assertEquals("PostgreSQL", connection.getProductName());
Assert.assertEquals("helpful-adroit-123456", connection.getSuppliedProperties().getProperty("Project"));
Assert.assertEquals("test-instance", connection.getSuppliedProperties().getProperty("Instance"));
Assert.assertEquals("test", connection.getSuppliedProperties().getProperty("Database"));
Assert.assertEquals("TEST", connection.getSuppliedProperties().getProperty("OAuthAccessToken"));
Assert.assertTrue(connection.isAllowExtendedMode());
Assert.assertEquals(subject.isDefaultAutoCommit(), connection.getAutoCommit());
PooledConnection con2 = subject.getPooledConnection("TEST", "TEST");
Assert.assertNotNull(con2);
boolean exception = false;
try
{
subject.getParentLogger();
}
catch (SQLFeatureNotSupportedException e)
{
exception = true;
}
Assert.assertTrue(exception);
}
示例8: wrapConnectionAndMarkAsInUse
import javax.sql.PooledConnection; //导入方法依赖的package包/类
private Connection wrapConnectionAndMarkAsInUse(
PooledConnection pooledConnection) throws SQLException {
pooledConnection = assureValidConnection(pooledConnection);
Connection conn = pooledConnection.getConnection();
if (doResetAutoCommit) {
conn.setAutoCommit(isAutoCommit);
}
if (doResetReadOnly) {
conn.setReadOnly(isReadOnly);
}
if (doResetTransactionIsolation) {
conn.setTransactionIsolation(transactionIsolation);
/* TESING ONLY!!
System.err.println("<<<<<<<<< ISO LVL => " + transactionIsolation
+ " >>>>>>>>>>>>");
*/
}
if (doResetCatalog) {
conn.setCatalog(catalog);
/* TESTING ONLY!
System.err.println("<<<<<<<<< CAT => " + catalog
+ " >>>>>>>>>>>>");
*/
}
if (validationQuery != null) {
// End-to-end test before return the Connection.
java.sql.ResultSet rs = null;
try {
rs = conn.createStatement().executeQuery(validationQuery);
if (!rs.next()) {
throw new SQLException("0 rows returned");
}
} catch (SQLException se) {
closePhysically(pooledConnection,
"Closing non-validating pooledConnection.");
throw new SQLException("Validation query failed: "
+ se.getMessage());
} finally {
if (rs != null) {
rs.close();
}
}
}
this.connectionsInUse.add(pooledConnection);
SessionConnectionWrapper sessionWrapper =
new SessionConnectionWrapper(pooledConnection.getConnection());
this.sessionConnectionWrappers.put(pooledConnection, sessionWrapper);
return sessionWrapper;
}
示例9: testChangeUserAndCharsets
import javax.sql.PooledConnection; //导入方法依赖的package包/类
/**
* 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();
}
}
示例10: queryTable
import javax.sql.PooledConnection; //导入方法依赖的package包/类
void queryTable(ChangeWriter changeWriter, ChangeKey changeKey) throws SQLException {
PooledConnection pooledConnection = null;
try {
pooledConnection = JdbcUtils.openPooledConnection(this.config, changeKey);
log.trace("{}: Setting transaction level to 4096 (READ_COMMITTED_SNAPSHOT)", changeKey);
pooledConnection.getConnection().setTransactionIsolation(4096);
pooledConnection.getConnection().setAutoCommit(false);
TableMetadataProvider.TableMetadata tableMetadata = this.tableMetadataProvider.tableMetadata(changeKey);
MsSqlQueryBuilder queryBuilder = new MsSqlQueryBuilder(pooledConnection.getConnection());
Map<String, Object> sourcePartition = Change.sourcePartition(changeKey);
Map<String, Object> startOffset = this.tableMetadataProvider.startOffset(changeKey);
long offset = MsSqlChange.offset(startOffset);
log.trace("{}: Starting at offset {} ", changeKey, offset);
try (PreparedStatement statement = queryBuilder.changeTrackingStatement(tableMetadata)) {
statement.setLong(1, offset);
long count = 0;
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
final long changeVersion = resultSet.getLong("__metadata_sys_change_version");
log.trace("{}: __metadata_sys_change_version = {}", changeKey, changeVersion);
MsSqlChange.Builder builder = MsSqlChange.builder();
MsSqlChange change = builder.build(tableMetadata, resultSet, this.time);
change.sourcePartition = sourcePartition;
change.sourceOffset = MsSqlChange.offset(changeVersion);
changeWriter.addChange(change);
this.tableMetadataProvider.cacheOffset(changeKey, change.sourceOffset);
count++;
}
}
log.info("{}: Processed {} record(s).", changeKey, count);
}
} finally {
if (null != pooledConnection) {
log.trace("{}: calling connection.commit()", changeKey);
pooledConnection.getConnection().commit();
}
JdbcUtils.closeConnection(pooledConnection);
}
}