public class BookDBAO {
Connection conn;
private ArrayList bookContainer;
private boolean conFree=true;
    public BookDBAO() throws Exception {
     try{
     Class.forName(Constant.dbConnStr[0]);
     conn=DriverManager.getConnection(Constant.dbConnStr[3],Constant.dbConnStr[1],Constant.dbConnStr[2]);
     }catch(Exception e){
     throw new Exception("Countn't get Connection Object!"+e.getMessage());
     }    
    }
    
    public void remove(){
     try {
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
    }
    
    public synchronized Connection getConection(){
     while (!conFree){
     try {
wait();
} catch (InterruptedException e) {
}
     }
     conFree=false;
     notify();
     return conn;
    }
    
    public synchronized void releaseConnection(){
     while(conFree){
     try {
wait();
} catch (InterruptedException e) {
}
     }
     conFree=true;
     notify();
    }
    说说getConnection,releaseConnection 这两个方法看不懂,不能理解。请大家说详细点。谢谢!

解决方案 »

  1.   

    conn=DriverManager.getConnection(Constant.dbConnStr[3],Constant.dbConnStr[1],Constant.dbConnStr[2]);这个是用来取得数据库联接的。因为你的程序不全我猜测你用的应该是JDBC:ODBC桥联接Constant应该是用来存放数据库URL地址和用户名用户密码的 一个静态数组。releaseConnection是用来释放这个联接的以避免一直占用资源。
      

  2.   

    synchronized 是同步机制 简单说来就是防止操作系统中的死锁出现。
    在你getConnection完成以后releaseConnection 才能对这个connection做操作
      

  3.   

    分析下,getConnection()能取得到连接,releaseConnection()就能够释放连接呀。