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


Java Connection.isClosed方法代码示例

本文整理汇总了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);
}
 
开发者ID:EixoX,项目名称:jetfuel,代码行数:35,代码来源:Database.java

示例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();
        }
    }
}
 
开发者ID:Microsoft,项目名称:elastic-db-tools-for-java,代码行数:37,代码来源:MultiShardTestUtils.java

示例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;
}
 
开发者ID:BriData,项目名称:DBus,代码行数:30,代码来源:OracleManager.java

示例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;
}
 
开发者ID:seyidkanan,项目名称:my-diploma-work,代码行数:20,代码来源:QueriesImpl.java

示例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;
}
 
开发者ID:seyidkanan,项目名称:my-diploma-work,代码行数:25,代码来源:QueriesImpl.java

示例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();
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:21,代码来源:AbandonPercentageTest.java

示例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;
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:26,代码来源:JDBCStore.java

示例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);
	}
}
 
开发者ID:experdb,项目名称:eXperDB-DB2PG,代码行数:15,代码来源:ExecuteQuery.java

示例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);
}
 
开发者ID:Microsoft,项目名称:elastic-db-tools-for-java,代码行数:23,代码来源:MultiShardResultSetTests.java

示例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;
		}
	}
}
 
开发者ID:yswang0927,项目名称:ralasafe,代码行数:14,代码来源:DBUtil.java

示例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);
        }
    }

}
 
开发者ID:actiontech,项目名称:dble,代码行数:35,代码来源:TravelRecordInsertJob.java

示例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;
}
 
开发者ID:seyidkanan,项目名称:my-diploma-work,代码行数:32,代码来源:QueriesImpl.java

示例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();
    }
}
 
开发者ID:Microsoft,项目名称:elastic-db-tools-for-java,代码行数:14,代码来源:SqlDatabaseUtils.java

示例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;
}
 
开发者ID:Microsoft,项目名称:elastic-db-tools-for-java,代码行数:30,代码来源:ShardMapperTests.java

示例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();
        }
    }
}
 
开发者ID:Microsoft,项目名称:elastic-db-tools-for-java,代码行数:41,代码来源:ShardMapManagerFactoryTests.java


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