📄 connectionpool.java
字号:
public class ConnectionPool
{
private ConnectionWrapper wrapper;
public void init()
{
Class.forName("oracle driver class name");
con = DriverManger.getConnection("url", "user", "pwd");
wrapper = new ConnectionWrapper(this, con);
}
public synchronized Connection getConnection()
{
while(wrapper.isBusy())
{
wait();
}
wrapper.setBusy(true);
return wrapper;
}
public synchronized Connection releaseConnection()
{
while(wrapper.isBusy())
{
wait();
}
nortifyAll();
}
public void destroy()
{
wrapper.release();
}
}
class ConnectionWrapper implements Connection
{
private Connection con;
private boolean inUse;
private ConnectionPool pool;
public ConnectionWrapper(ConnectionPool pool, Connection con)
{
this.con = con;
this.pool = pool;
}
public void setBusy()
{
inUse = true;
}
public boolean isBusy()
{
return inUse;
}
public Statement createStatement() throws SQLException
{
return con.createStatement();
}
public void close() throws SQLException
{
inUse = false;
pool.releaseConnection();
}
public void release()
{
con.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -