[英]How to use connection pooling
我是連接池的新手。我在mysql中創建了一個連接池,該連接池添加了五個連接。現在我想知道連接池的應用是什么,即在創建該池后如何使用它。這里
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;
import com.mysql.jdbc.Connection;
class ConnectionPoolManager {
String databaseUrl = "jdbc:mysql://localhost:3306/homeland";
String userName = "root";
String password = "root";
Vector connectionPool = new Vector();
public ConnectionPoolManager() {
initialize();
}
public ConnectionPoolManager(
// String databaseName,
String databaseUrl, String userName, String password) {
this.databaseUrl = databaseUrl;
this.userName = userName;
this.password = password;
initialize();
}
private void initialize() {
// Here we can initialize all the information that we need
initializeConnectionPool();
}
private void initializeConnectionPool() {
while (!checkIfConnectionPoolIsFull()) {
System.out
.println("Connection Pool is NOT full. Proceeding with adding new connections");
// Adding new connection instance until the pool is full
connectionPool.addElement(createNewConnectionForPool());
}
System.out.println("Connection Pool is full.");
}
private synchronized boolean checkIfConnectionPoolIsFull() {
final int MAX_POOL_SIZE = 5;
// Check if the pool size
if (connectionPool.size() < 5) {
return false;
}
return true;
}
// Creating a connection
private Connection createNewConnectionForPool() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) DriverManager.getConnection(databaseUrl,
userName, password);
System.out.println("Connection: " + connection);
} catch (SQLException sqle) {
System.err.println("SQLException: " + sqle);
return null;
} catch (ClassNotFoundException cnfe) {
System.err.println("ClassNotFoundException: " + cnfe);
return null;
}
return connection;
}
public synchronized Connection getConnectionFromPool() {
Connection connection = null;
// Check if there is a connection available. There are times when all
// the connections in the pool may be used up
if (connectionPool.size() > 0) {
connection = (Connection) connectionPool.firstElement();
connectionPool.removeElementAt(0);
}
// Giving away the connection from the connection pool
return connection;
}
public synchronized void returnConnectionToPool(Connection connection) {
// Adding the connection from the client back to the connection pool
connectionPool.addElement(connection);
}
public static void main(String args[]) {
new ConnectionPoolManager();
}
}
有人可以幫忙嗎?
連接池的目的是維護與數據庫的許多打開的連接,以便當您的應用程序需要連接時,它不必經歷可能耗費資源和時間的打開新連接的過程。
當應用程序需要數據庫連接時,它將從池中“借用”一個。 完成后,它會退還給您,以后可以重新使用該連接。
一旦獲得連接,就可以通過JDBC(Java數據庫連接)API在應用程序中使用它。
可以在http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html上找到Oracle使用JDBC的基本教程。
要記住的另一件事是,很多工作已經投入到開發連接池中,並且可能不需要重新發明輪子,除非可能是作為學習練習。 Apache Tomcat的連接池實現可以在Tomcat之外使用(例如,在獨立的Java應用程序中),並且相當靈活且易於配置。 可以在https://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html中找到
我會說代碼很容易解釋。
您個人創建了一個池實例,我更喜歡使用單例,但這是另一個主題
ConnectionPoolManager connectionPoolManager = new ConnectionPoolManager();
現在,每個想要建立連接的機構都需要向該管理器提供參考。 必要時,您可以請求池中的免費連接...
public void queryDatabaseForStuff(ConnectionPoolManager cpm) throws SQLException {
Connection con = cpm.getConnectionFromPool();
//....
連接完成后,將其傳遞回經理...
try {
//...
} finally {
cmp.returnConnectionToPool(con);
}
現在。 您可能想研究一個阻止過程,該過程將在池為空時阻止當前對getConnectionFromPool
調用,這意味着它將拋出異常(如果要包含超時功能)或有效連接。
重新池化Connection
,您可能想檢查Connection
是否已關閉,並進行某種復興,以確保池的容量接近容量...
請查看此鏈接以獲取詳細的答案-https: //examples.javacodegeeks.com/core-java/sql/jdbc-connection-pool-example/
您無需重新創建Connection對象池,而請使用Apache提供的庫。 請清除以下內容:1-為什么以及為什么讓您想到連接池? 2-在您的Maven項目中使用以下Apache commons-dbcp lib,然后根據文檔使用這些類。 3.這樣可以解決您所有的問題嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.