本文整理匯總了Java中java.sql.Connection.isClosed方法的典型用法代碼示例。如果您正苦於以下問題:Java Connection.isClosed方法的具體用法?Java Connection.isClosed怎麽用?Java Connection.isClosed使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.sql.Connection
的用法示例。
在下文中一共展示了Connection.isClosed方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: popConnection
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Pops a connection from the pool and checks if it is open and the last
* used time is below 19 minutes. If those things are false, it attempts to
* close the connection. Otherwise it returns the connection or simply
* instantiates a new one;
*
* @return
* @throws SQLException
*/
public synchronized Connection popConnection() throws SQLException {
if (this.transaction_connection != null)
return this.transaction_connection;
else if (!connection_pool.isEmpty()) {
Connection conn = connection_pool.removeFirst();
Date conn_used = connection_last_used.removeFirst();
if (!conn.isClosed()) {
// 19 minutes threshold
if (new Date().getTime() - conn_used.getTime() <= 1140000)
return conn;
// just close it and do nothing
try {
conn.close();
} catch (Exception e) {
}
} // try to pop another one;
return popConnection();
}
return properties == null
? DriverManager.getConnection(url)
: DriverManager.getConnection(url, properties);
}
示例2: changeColumnNameOnShardedTable
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Helper method that alters the column name on one of our test tables in one of our test databases. Useful for inducing a schema mismatch to test
* our failure handling.
*
* @param database
* The 0-based index of the test database to change the schema in.
* @param oldColName
* The current name of the column to change.
* @param newColName
* The desired new name of the column.
*/
static void changeColumnNameOnShardedTable(int database,
String oldColName,
String newColName) throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(getTestConnectionString(testDatabaseNames.get(database)));
try (Statement stmt = conn.createStatement()) {
String tsql = String.format("EXEC sp_rename '[%1$s].[%2$s]', '%3$s', 'COLUMN';", TABLE_NAME, oldColName, newColName);
stmt.executeUpdate(tsql);
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
catch (Exception e) {
System.out.printf("Failed to connect to SQL database: " + e.getMessage());
}
finally {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
示例3: getConnection
import java.sql.Connection; //導入方法依賴的package包/類
/**
* @return a Connection instance that can be used to connect to the
* given database, if a previously-opened connection is available in
* the cache. Returns null if none is available in the map.
*/
public synchronized Connection getConnection(String connectStr,
String username) throws SQLException {
CacheKey key = new CacheKey(connectStr, username);
Connection cached = connectionMap.get(key);
if (null != cached) {
connectionMap.remove(key);
if (cached.isReadOnly()) {
// Read-only mode? Don't want it.
cached.close();
}
if (cached.isClosed()) {
// This connection isn't usable.
return null;
}
cached.rollback(); // Reset any transaction state.
cached.clearWarnings();
LOG.debug("Got cached connection for " + key);
}
return cached;
}
示例4: getInfoAboutEatWith
import java.sql.Connection; //導入方法依賴的package包/類
@Override
public List<Eat> getInfoAboutEatWith(int eatId) throws SQLException {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = util.createSqlInfoEatWith(eatId);
System.out.println(sql);
List<Eat> eatList = new ArrayList<>();
c = connectionDB.getConnection();
if (!c.isClosed()) {
ps = c.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
eatList.add(new Eat(rs.getString(1), rs.getString(2), rs.getString(3)));
}
}
connectionDB.closeConnection(c, ps, rs);
return eatList;
}
示例5: getEatList
import java.sql.Connection; //導入方法依賴的package包/類
@Override
public List<Eat> getEatList() throws SQLException {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select id,name from eat_name";
System.out.println(sql);
List<Eat> eatList = new ArrayList<>();
try {
c = connectionDB.getConnection();
if (c.isClosed() == false) {
ps = c.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
eatList.add(new Eat(rs.getInt("id"), rs.getString("name")));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connectionDB.closeConnection(c, ps, rs);
}
return eatList;
}
示例6: testResetConnection
import java.sql.Connection; //導入方法依賴的package包/類
@Test
public void testResetConnection() throws Exception {
int size = 1;
this.datasource.setMaxActive(size);
this.datasource.setMaxIdle(size);
this.datasource.setInitialSize(0);
this.datasource.getPoolProperties().setAbandonWhenPercentageFull(100);
this.datasource.getPoolProperties().setTimeBetweenEvictionRunsMillis(100);
this.datasource.getPoolProperties().setRemoveAbandoned(true);
this.datasource.getPoolProperties().setRemoveAbandonedTimeout(1);
this.datasource.getPoolProperties().setJdbcInterceptors(ResetAbandonedTimer.class.getName());
Connection con = datasource.getConnection();
Assert.assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
for (int i=0; i<20; i++) {
Thread.sleep(200);
con.isClosed();
}
Assert.assertEquals("Number of connections active/busy should be 1",1,datasource.getPool().getActive());
con.close();
}
示例7: getConnection
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Check the connection associated with this store, if it's
* <code>null</code> or closed try to reopen it.
* Returns <code>null</code> if the connection could not be established.
*
* @return <code>Connection</code> if the connection succeeded
*/
protected Connection getConnection() {
Connection conn = null;
try {
conn = open();
if (conn == null || conn.isClosed()) {
manager.getContainer().getLogger().info(sm.getString(getStoreName() + ".checkConnectionDBClosed"));
conn = open();
if (conn == null || conn.isClosed()) {
manager.getContainer().getLogger().info(sm.getString(getStoreName() + ".checkConnectionDBReOpenFail"));
}
}
} catch (SQLException ex) {
manager.getContainer().getLogger().error(sm.getString(getStoreName() + ".checkConnectionSQLException",
ex.toString()));
}
return conn;
}
示例8: CloseConn
import java.sql.Connection; //導入方法依賴的package包/類
protected static void CloseConn(Connection conn, PreparedStatement pStmt) {
try{
if(pStmt != null) {
pStmt.close();
}
if (conn != null && !conn.isClosed()) {
conn.commit();
conn.close();
conn = null;
}
}catch(Exception e){
LogUtils.error(e.getMessage(),ExecuteQuery.class,e);
}
}
示例9: getReader
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Gets a ResultSet by executing the passed in t-sql over the passed in connection.
*
* @param conn
* Connection to the database we wish to execute the t-sql against.
* @param tsql
* The t-sql to execute.
* @return The ResultSet obtained by executing the passed in t-sql over the passed in connection.
*/
private LabeledResultSet getReader(Connection conn,
String tsql,
String dbName) throws SQLException {
String connStr = conn.getMetaData().getURL();
SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder(connStr);
if (conn.isClosed()) {
conn = DriverManager.getConnection(connStr);
}
Statement cmd = conn.createStatement();
ResultSet sdr = cmd.executeQuery(tsql);
return new LabeledResultSet(sdr, new ShardLocation(connStrBldr.getDataSource(), dbName), cmd);
}
示例10: close
import java.sql.Connection; //導入方法依賴的package包/類
public static void close(Connection conn) {
if (conn != null) {
try {
if (!conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}
示例11: run
import java.sql.Connection; //導入方法依賴的package包/類
@Override
public void run() {
Connection con = null;
try {
List<Map<String, String>> batch = getNextBatch();
while (!batch.isEmpty()) {
try {
if (con == null || con.isClosed()) {
con = conPool.getConnection();
con.setAutoCommit(false);
}
insert(con, batch);
finshiedCount.addAndGet(batch.size());
} catch (Exception e) {
e.printStackTrace();
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
e1.printStackTrace();
}
failedCount.addAndGet(batch.size());
}
batch = getNextBatch();
}
} finally {
if (con != null) {
this.conPool.returnCon(con);
}
}
}
示例12: insertEat
import java.sql.Connection; //導入方法依賴的package包/類
@Override
public int insertEat(String eatName) {
int id = 0;
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "insert into eat_name(name)values(?)";
System.out.println(sql);
c = connectionDB.getConnection();
try {
if (!c.isClosed()) {
ps = c.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
ps.setString(1, eatName);
ps.executeUpdate();
rs = ps.getGeneratedKeys();
if (rs.next()) {
id = rs.getInt(1);
}
} else {
System.out.println("Connection is null");
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
connectionDB.closeConnection(c, ps, null);
} catch (Exception e) {
}
}
return id;
}
示例13: connFinally
import java.sql.Connection; //導入方法依賴的package包/類
private static void connFinally(Connection conn) {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
else {
ConsoleUtils.writeWarning("Returned Connection was either null or already closed.");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
示例14: tryOpenConnectionForKey
import java.sql.Connection; //導入方法依賴的package包/類
private boolean tryOpenConnectionForKey(ListShardMap lsm,
boolean openAsync) {
boolean failed = false;
Connection conn = null;
try {
if (openAsync) {
conn = lsm.openConnectionForKeyAsync(2, Globals.SHARD_USER_CONN_STRING).call();
}
else {
conn = lsm.openConnectionForKey(2, Globals.SHARD_USER_CONN_STRING);
}
}
catch (Exception ex) {
if (ex.getCause() != null && ex.getCause() instanceof SQLException) {
failed = true;
}
}
finally {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
return failed;
}
示例15: shardMapManagerFactoryTestsInitialize
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Initializes common state for tests in this class.
*/
@BeforeClass
public static void shardMapManagerFactoryTestsInitialize() throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(Globals.SHARD_MAP_MANAGER_TEST_CONN_STRING);
// Create ShardMapManager database
try (Statement stmt = conn.createStatement()) {
String query = String.format(Globals.CREATE_DATABASE_QUERY, Globals.SHARD_MAP_MANAGER_DATABASE_NAME);
stmt.executeUpdate(query);
}
catch (SQLException ex) {
ex.printStackTrace();
}
/*
* Testing TryGetSqlShardMapManager failure case here instead of in TryGetShardMapManager_Fail() There is no method to cleanup GSM
* objects, so if some other test runs in lab before TryGetShardMapManager_Fail, then this call will actually succeed as it will find
* earlier SMM structures. Calling it just after creating database makes sure that GSM does not exist. Other options were to recreate SMM
* database in tests (this will increase test duration) or delete storage structures (t-sql delete) in the test which is not very clean
* solution.
*/
ShardMapManager smm = null;
ReferenceObjectHelper<ShardMapManager> smmref = new ReferenceObjectHelper<>(smm);
boolean lookupSmm = ShardMapManagerFactory.tryGetSqlShardMapManager(Globals.SHARD_MAP_MANAGER_CONN_STRING,
ShardMapManagerLoadPolicy.Eager, RetryBehavior.getDefaultRetryBehavior(), smmref);
assertFalse(lookupSmm);
}
catch (Exception e) {
System.out.printf("Failed to connect to SQL database with connection string: " + e.getMessage());
}
finally {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}