大家看我這段程式碼有什麼問題麼?
package org.jelly.pool.impl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jelly.pool.AbstractConnectionPool;
import org.jelly.pool.exceptions.ServerShutDownException;
public class ConnectionPoolImpl extends AbstractConnectionPool {
private static Log logger = LogFactory.getLog(ConnectionPoolImpl.class);
// jdbc相當的引數宣告
private String jdbcUserName;
private String jdbcPassword;
private String jdbcUrl;
private String jdbcDriverClass;
// 連線池大小的控制變數宣告
private int minimum;
private int maximum;
private static final int DEFAULT_SIZE = 100;
private static final int DEFAULT_MIN_SIZE = 100;
private static final int DEFAULT_MAX_SIZE = 300;
private static final int DEFAULT_WAIT_TIMEOUT = 1000;
private static final String DEFAULT_METHOD_CLOSE = "close";
// 存放資料庫連線的集合
private List usedConnection;
private List availableConnection;
// 連線池的標誌位
private boolean isShutDown = false;
// 執行緒對像
private Thread woker = null;
// lock object
Object lock = new Object();
public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass, int minimum, int maximum) {
this.jdbcUserName = jdbcUserName;
this.jdbcPassword = jdbcPassword;
this.jdbcUrl = jdbcUrl;
this.jdbcDriverClass = jdbcDriverClass;
this.minimum = minimum;
this.maximum = maximum;
usedConnection = new LinkedList();
availableConnection = new LinkedList();
System.out.println("connection pool is started...");
System.out.println("jdbcUserName: " + jdbcUserName);
System.out.println("jdbcPassword: " + jdbcPassword);
System.out.println("jdbcUrl: " + jdbcUrl);
System.out.println("jdbcDriverClass: " + jdbcDriverClass);
try {
Driver driver = (Driver)Class.forName(jdbcDriverClass).newInstance();
DriverManager.registerDriver(driver);
makeMinimumConnection(minimum);
} catch (SQLException ex) {
throw new RuntimeException("cannot register jdbc driver or cannot get conection.");
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException("cannot found driver class: ['" + jdbcDriverClass + "'].");
} catch(InstantiationException ie) {
throw new RuntimeException("canoot instance object");
} catch(IllegalAccessException lae) {
throw new RuntimeException("access dined");
}
startThread();
}
// public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass, int minimum) {
// this(jdbcUserName, jdbcPassword, jdbcUrl, jdbcDriverClass, minimum, DEFAULT_MAX_SIZE);
// }
//
// public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass, int maximum) {
// this(jdbcUserName, jdbcPassword, jdbcUrl, jdbcDriverClass, minimum, DEFAULT_MIN_SIZE);
// }
public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass) {
this(jdbcUserName, jdbcPassword, jdbcUrl, jdbcDriverClass, DEFAULT_MIN_SIZE, DEFAULT_MAX_SIZE);
}
// 啟動連線池執行緒,子類必須H這個方法.
protected void startThread() {
woker = new Thread(this);
System.out.println("start thread...");
woker.start();
}
// 建立最小連線的數.
protected int makeMinimumConnection(int minimum) throws SQLException {
for (int i = 0; i < minimum; i++) {
System.out.println("iterator i: " + i);
availableConnection.add(makeConnection());
}
return 0;
}
protected void loop() {
System.out.println("loop");
while (!isShutDown) {
if (minimum < getAvailableSize()) {
for (int i = 0; i < getAvailableSize(); i++) {
Connection conn = (Connection)availableConnection.remove(0);
try {
conn.close();
} catch (SQLException ex) {
throw new RuntimeException("cannot close connection");
}
}
} else {
for (int i = getAvailableSize(); i < minimum; i++) {
try {
availableConnection.add(makeConnection());
} catch (SQLException ex) {
throw new RuntimeException("cannot create connection for pool");
}
}
}
try {
lock.wait(DEFAULT_WAIT_TIMEOUT);
} catch (InterruptedException ie) {
throw new RuntimeException("error");
}
System.out.println("available connection total is: " + getAvailableSize());
}
}
protected void shutDown() {
}
private Connection makeConnection() throws SQLException {
//_Connection conn = null;
if (jdbcUserName.length() > 0) {
System.out.println("create connection proxy for pool");
return new _Connection(DriverManager.getConnection( jdbcUrl, jdbcUserName, jdbcPassword)).getConnection();
} else {
return new _Connection(DriverManager.getConnection(jdbcUrl)).getConnection();
}
}
private Connection getInternalConnection() {
Connection result = null;
if (!availableConnection.isEmpty()) {
result = (Connection)availableConnection.remove(0);
} else if (usedConnection.size() < maximum) {
try {
result = makeConnection();
} catch (SQLException ex) {
throw new RuntimeException("cannot create connection for pool");
}
}
if (null != result) {
usedConnection.add(result);
}
return result;
}
private int getAvailableSize() {
return availableConnection.size();
}
private int getTotal() {
return (availableConnection.size() + usedConnection.size());
}
public synchronized Connection getConnection() {
if (isShutDown) {
throw new ServerShutDownException("the connection pool was shut down...");
}
Connection result = getInternalConnection();
return result;
}
public void exit() {
isShutDown = true;
}
public int getMinimum() {
return this.minimum;
}
public int getMaximum() {
return this.maximum;
}
/* (non-Javadoc)
* @see org.jelly.pool.ConnectionPool#getConnectionWaitTime(int)
*/
public Connection getConnectionWaitTime(int time) {
// TODO Auto-generated method stub
return null;
}
private class _Connection implements InvocationHandler {
private Connection conn;
public _Connection(Connection conn) {
this.conn = conn;
}
public Connection getConnection() {
//this.conn = conn;
return (Connection)Proxy.newProxyInstance( conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method m, Object[] args) {
System.out.println(m.getName());
Object obj = null;
if (DEFAULT_METHOD_CLOSE.equals(m.getName()) && minimum > getAvailableSize()) {
availableConnection.add(conn);
lock.notify();
} else {
try {
obj = m.invoke(conn, args);
}catch(IllegalAccessException el) {
}catch(IllegalArgumentException ia) {
}catch(InvocationTargetException ite) {
}
}
return obj;
}
}
}
lock.wait();這一句說
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException: current thread not owner
是什麼意思呢?
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jelly.pool.AbstractConnectionPool;
import org.jelly.pool.exceptions.ServerShutDownException;
public class ConnectionPoolImpl extends AbstractConnectionPool {
private static Log logger = LogFactory.getLog(ConnectionPoolImpl.class);
// jdbc相當的引數宣告
private String jdbcUserName;
private String jdbcPassword;
private String jdbcUrl;
private String jdbcDriverClass;
// 連線池大小的控制變數宣告
private int minimum;
private int maximum;
private static final int DEFAULT_SIZE = 100;
private static final int DEFAULT_MIN_SIZE = 100;
private static final int DEFAULT_MAX_SIZE = 300;
private static final int DEFAULT_WAIT_TIMEOUT = 1000;
private static final String DEFAULT_METHOD_CLOSE = "close";
// 存放資料庫連線的集合
private List usedConnection;
private List availableConnection;
// 連線池的標誌位
private boolean isShutDown = false;
// 執行緒對像
private Thread woker = null;
// lock object
Object lock = new Object();
public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass, int minimum, int maximum) {
this.jdbcUserName = jdbcUserName;
this.jdbcPassword = jdbcPassword;
this.jdbcUrl = jdbcUrl;
this.jdbcDriverClass = jdbcDriverClass;
this.minimum = minimum;
this.maximum = maximum;
usedConnection = new LinkedList();
availableConnection = new LinkedList();
System.out.println("connection pool is started...");
System.out.println("jdbcUserName: " + jdbcUserName);
System.out.println("jdbcPassword: " + jdbcPassword);
System.out.println("jdbcUrl: " + jdbcUrl);
System.out.println("jdbcDriverClass: " + jdbcDriverClass);
try {
Driver driver = (Driver)Class.forName(jdbcDriverClass).newInstance();
DriverManager.registerDriver(driver);
makeMinimumConnection(minimum);
} catch (SQLException ex) {
throw new RuntimeException("cannot register jdbc driver or cannot get conection.");
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException("cannot found driver class: ['" + jdbcDriverClass + "'].");
} catch(InstantiationException ie) {
throw new RuntimeException("canoot instance object");
} catch(IllegalAccessException lae) {
throw new RuntimeException("access dined");
}
startThread();
}
// public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass, int minimum) {
// this(jdbcUserName, jdbcPassword, jdbcUrl, jdbcDriverClass, minimum, DEFAULT_MAX_SIZE);
// }
//
// public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass, int maximum) {
// this(jdbcUserName, jdbcPassword, jdbcUrl, jdbcDriverClass, minimum, DEFAULT_MIN_SIZE);
// }
public ConnectionPoolImpl(String jdbcUserName, String jdbcPassword, String jdbcUrl, String jdbcDriverClass) {
this(jdbcUserName, jdbcPassword, jdbcUrl, jdbcDriverClass, DEFAULT_MIN_SIZE, DEFAULT_MAX_SIZE);
}
// 啟動連線池執行緒,子類必須H這個方法.
protected void startThread() {
woker = new Thread(this);
System.out.println("start thread...");
woker.start();
}
// 建立最小連線的數.
protected int makeMinimumConnection(int minimum) throws SQLException {
for (int i = 0; i < minimum; i++) {
System.out.println("iterator i: " + i);
availableConnection.add(makeConnection());
}
return 0;
}
protected void loop() {
System.out.println("loop");
while (!isShutDown) {
if (minimum < getAvailableSize()) {
for (int i = 0; i < getAvailableSize(); i++) {
Connection conn = (Connection)availableConnection.remove(0);
try {
conn.close();
} catch (SQLException ex) {
throw new RuntimeException("cannot close connection");
}
}
} else {
for (int i = getAvailableSize(); i < minimum; i++) {
try {
availableConnection.add(makeConnection());
} catch (SQLException ex) {
throw new RuntimeException("cannot create connection for pool");
}
}
}
try {
lock.wait(DEFAULT_WAIT_TIMEOUT);
} catch (InterruptedException ie) {
throw new RuntimeException("error");
}
System.out.println("available connection total is: " + getAvailableSize());
}
}
protected void shutDown() {
}
private Connection makeConnection() throws SQLException {
//_Connection conn = null;
if (jdbcUserName.length() > 0) {
System.out.println("create connection proxy for pool");
return new _Connection(DriverManager.getConnection( jdbcUrl, jdbcUserName, jdbcPassword)).getConnection();
} else {
return new _Connection(DriverManager.getConnection(jdbcUrl)).getConnection();
}
}
private Connection getInternalConnection() {
Connection result = null;
if (!availableConnection.isEmpty()) {
result = (Connection)availableConnection.remove(0);
} else if (usedConnection.size() < maximum) {
try {
result = makeConnection();
} catch (SQLException ex) {
throw new RuntimeException("cannot create connection for pool");
}
}
if (null != result) {
usedConnection.add(result);
}
return result;
}
private int getAvailableSize() {
return availableConnection.size();
}
private int getTotal() {
return (availableConnection.size() + usedConnection.size());
}
public synchronized Connection getConnection() {
if (isShutDown) {
throw new ServerShutDownException("the connection pool was shut down...");
}
Connection result = getInternalConnection();
return result;
}
public void exit() {
isShutDown = true;
}
public int getMinimum() {
return this.minimum;
}
public int getMaximum() {
return this.maximum;
}
/* (non-Javadoc)
* @see org.jelly.pool.ConnectionPool#getConnectionWaitTime(int)
*/
public Connection getConnectionWaitTime(int time) {
// TODO Auto-generated method stub
return null;
}
private class _Connection implements InvocationHandler {
private Connection conn;
public _Connection(Connection conn) {
this.conn = conn;
}
public Connection getConnection() {
//this.conn = conn;
return (Connection)Proxy.newProxyInstance( conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method m, Object[] args) {
System.out.println(m.getName());
Object obj = null;
if (DEFAULT_METHOD_CLOSE.equals(m.getName()) && minimum > getAvailableSize()) {
availableConnection.add(conn);
lock.notify();
} else {
try {
obj = m.invoke(conn, args);
}catch(IllegalAccessException el) {
}catch(IllegalArgumentException ia) {
}catch(InvocationTargetException ite) {
}
}
return obj;
}
}
}
lock.wait();這一句說
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException: current thread not owner
是什麼意思呢?
相關文章
- jive中這段程式碼什麼意思?
- 大家快看哪,JClass的一段原始碼,不是程式碼有問題就是我有問題,不過我想我沒問題。原始碼
- 都100%程式碼覆蓋了,還會有什麼問題?
- 什麼是低程式碼?低程式碼平臺能解決什麼樣的問題?
- 為什麼會有這麼多程式語言
- 為什麼會有這麼多的程式語言?
- 什麼是程式碼審計?程式碼審計有什麼好處?
- 這是什麼騷批程式碼!
- python有什麼特性?為什麼這麼火?Python
- 這次面試就差不多了,你有什麼問題需要問我呢?面試
- RFM是什麼?這個模型有什麼用?模型
- 為什麼有這麼多 Python?Python
- 你構建的程式碼為什麼這麼大
- 我請教大家這個平常的問題怎麼處理的
- 低程式碼有什麼應用前景?
- 這兒有一個資料連線,大家看這兒有哪些問題
- 想問下大家Mac有沒有什麼好用的檢視日誌的工具Mac
- 這段程式碼到底怎麼走?終於搞定Event loopOOP
- that=this這樣的程式碼的作用是什麼
- [提問交流]下面這段程式碼中沒帶註釋的那3行是什麼意思呢?謝謝!!!
- 為什麼這段程式碼輸入一個數之後顯示不了結果
- [提問交流]有段程式碼沒看懂
- 搜尋和其他機器學習問題有什麼不同?機器學習
- 伺服器過載會有什麼問題伺服器
- 為什麼安裝jdonNews時mysql有問題MySql
- 低程式碼與專業程式碼有什麼區別?
- 這是什麼這是什麼
- java程式碼規範有什麼意義Java
- 低程式碼平臺有什麼價值?
- Python為什麼這麼火?學習python有什麼用?Python
- 請問getKey()方法是什麼,有什麼用
- 這個問題的思考方向應該是什麼??????
- Python這麼火,它與其他程式語言有什麼區別?Python
- 大家都用什麼web框架Web框架
- 微信小程式是什麼 微信小程式有什麼用?微信小程式
- 【程式碼實驗室】.->和.有什麼區別?
- 除了敲程式碼,你還有什麼副業嗎?
- RAG能解決大模型的什麼問題?不能解決什麼問題?大模型