我已經寫了一個Java函數,它運行MySQL查詢並返回結果。我在這裏使用這種方法實現了連接池:http://www.kodejava.org/how-do-i-create-a-database-connection-pool/。該函數正在工作,但連接時間仍然與沒有合併約190毫秒相同。有人可以告訴我我做錯了什麼嗎?Java MySQL連接池不工作
這是我的代碼:
public static ArrayList<Map<String,Object>> query(String q) throws Exception {
long start, end;
GenericObjectPool connectionPool = null;
String DRIVER = "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://localhost/dbname";
String USER = "root";
String PASS = "";
Class.forName(DRIVER).newInstance();
connectionPool = new GenericObjectPool();
connectionPool.setMaxActive(10);
ConnectionFactory cf = new DriverManagerConnectionFactory(URL, USER, PASS);
PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, connectionPool, null, null, false, true);
DataSource ds = new PoolingDataSource(connectionPool);
//create statement
Statement Stm = null;
try {
Connection Con = null;
PreparedStatement stmt = null;
start = System.currentTimeMillis();
Con = ds.getConnection();
end = System.currentTimeMillis();
System.out.println("DB Connection: " + Long.toString(end - start) + " ms");
//fetch out rows
ArrayList<Map<String, Object>> Rows = new ArrayList<Map<String,Object>>();
Stm = Con.createStatement();
//query
ResultSet Result = null;
boolean Returning_Rows = Stm.execute(q);
if (Returning_Rows) {
Result = Stm.getResultSet();
} else {
return new ArrayList<Map<String,Object>>();
}
//get metadata
ResultSetMetaData Meta = null;
Meta = Result.getMetaData();
//get column names
int Col_Count = Meta.getColumnCount();
ArrayList<String> Cols = new ArrayList<String>();
for (int Index=1; Index<=Col_Count; Index++) {
Cols.add(Meta.getColumnName(Index));
}
while (Result.next()) {
HashMap<String,Object> Row = new HashMap<String,Object>();
for (String Col_Name:Cols) {
Object Val = Result.getObject(Col_Name);
Row.put(Col_Name,Val);
}
Rows.add(Row);
}
//close statement
Stm.close();
//pass back rows
return Rows;
} catch (Exception Ex) {
System.out.print(Ex.getMessage());
return new ArrayList<Map<String,Object>>();
} finally {
if (Stm != null) {
Stm.close();
}
if (Stm != null) {
Stm.close();
}
System.out.println("Max connections: " + connectionPool.getMaxActive());
System.out.println("Active connections: " + connectionPool.getNumActive());
System.out.println("Idle connections: " + connectionPool.getNumIdle());
}
}
這是控制檯輸出每次:
DB Connection: 186 ms
Max connections: 10
Active connections: 1
Idle connections: 0
更新:我應該注意,使用該工作原理是這樣的Java應用程序:執行,只運行一個查詢並關閉。我想如果PHP像這樣工作,並且默認使用連接池,那麼Java應該如何?如我錯了請糾正我。
再一次,不需要爲運行一次應用程序創建連接池。如果您只是堅持一個連接的引用,那麼這是一個大小爲1的連接池。我認爲您對PHP頁面的性能與您自己的Java應用程序之間的200ms差異感到癡迷。 – ktm5124
問題是,它是高度使用的應用程序,我說它每次在大系統上加載任何頁面時都會運行...每天數十萬次。我想刮掉完全浪費的200毫秒。 – Caballero
可能重複[PHP vs Java MySQL連接池](http://stackoverflow.com/questions/18105989/php-vs-java-mysql-connection-pooling) – RandomSeed