Class.forName(driver);
dbDriver = (Driver) Class.forName(driver).newInstance();
DriverManager.setLoginTimeout(10);
conn = dbDriver.connect(url, props);程序运行一段时间后,出现僵死情况,不知何因?请高手出手相助,谢谢!

解决方案 »

  1.   

    每个一段时间去连接数据库?这个需求有点搞怪了!
    连接数据库的资源本来就很宝贵,你还隔一段时间连接一次。能保证你在连接的那段时间你的程序正常执行?
    你这段代码在一段时间请求连接,同样的对象,也许没有释放你就又进行重新连接了,这个很容易造成数据库的瓶颈。楼主建议使用连接池来获取数据库的连接。
    package com.bean.db;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;public class ConnectionProvider {
    // Define the connection information
    private static final String DBURL = "jdbc:mysql://localhost:3306/eshop?useUnicode=true&characterEncoding=UTF-8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static final String DRIVER = "com.mysql.jdbc.Driver"; // Define the max of the pool
    private static final int POOL_SIZE = 5; // Define the connection pool
    private Map<Connection, String> connectionPool = null; // Define the singleton
    private static ConnectionProvider connectionProvider = new ConnectionProvider(); /**
     * When get the singleton this method will be invoke once
     * So the connection just create once
     */
    private void initPool() {
    try {
    connectionPool = new HashMap<Connection, String>();
    Class.forName(DRIVER);
    Connection conn = DriverManager.getConnection(DBURL, USERNAME,
    PASSWORD);
    for (int poolIndex = 0; poolIndex < POOL_SIZE; poolIndex++) {
    connectionPool.put(conn, "AVAILABLE");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } private ConnectionProvider() {
    initPool();
    } // The singleton method
    public static ConnectionProvider instanceConnectionProvider() {
    if (connectionProvider == null) {
    connectionProvider = new ConnectionProvider();
    }
    return connectionProvider;
    } // Get the connection
    public Connection getConnection() throws ClassNotFoundException,
    SQLException {
    boolean isConnectionAvailable = true;
    for (Entry<Connection, String> entry : connectionPool.entrySet()) {
    synchronized (entry) {
    if ("AVAILABLE".equals(entry.getValue())) {
    entry.setValue("NOTAVAILABLE");
    return (Connection) entry.getKey();
    }
    isConnectionAvailable = false;
    }
    } // Not enough connection
    if (!isConnectionAvailable) {
    Class.forName(DRIVER);
    Connection connection = DriverManager.getConnection(DBURL,
    USERNAME, PASSWORD);
    connectionPool.put(connection, "NOTAVAILABLE");
    return connection;
    } return null;
    } // Close connection
    public void closeConnection(Connection connection) throws SQLException {
    for (Entry<Connection, String> entry : connectionPool.entrySet()) {
    synchronized (entry) {
    if (entry.getKey().equals(connection)) {
    entry.setValue("AVAILABLE");
    }
    }
    }
    }
    }
      

  2.   

    主要是定期检查数据库连接是否可用,而且每次都会在finally中close的,所以很奇怪。10个数据库连接,之前8个可以通的数据库连接在上述代码部分僵死了(后边的日志都不再打印)。之前2个不通的数据库连接。还是正常打印日志。所以,我都不相信科学了,超时竟然都退不出来
      

  3.   

    查看oracle的session,是不是太多了