독립 실행 형 (웹이 아닌) Java 응용 프로그램에서 연결 풀을 사용해야합니다. 내가 일하는 곳에서는 보안 계층을 거치지 않고 API를 사용할 수 없으므로 곧 완료해야합니다. 아래는이 연결 풀을 만드는 나의 시도입니다.독립 실행 형 Java 응용 프로그램에서 연결 풀 만들기
이 코드를 단위 테스트하고 전체 응용 프로그램의 컨텍스트 내에서 수 백 번 테스트했으며 모든 경우에 테스트가 오류없이 통과했으며 각 실행의 성능이 3 천 배 미만으로 빠릅니다. 간단한 연결, 데이터를 검색, 직렬 방식으로 연결 해제; 그러나 나는 아직도이 접근법에 문제가있어 아직 내가 발굴하지 못했다는 우려는 고집 스럽다. 나는 누군가에게 아래의 코드에 관해 조언 해 주시면 감사하겠습니다. 이것은이 사이트의 첫 번째 게시물입니다. 에티켓에 실수를 저지른 경우 알려 주시기 바랍니다. 게시하기 전에이 문제에 대해이 사이트를 검색했습니다. 호출 예제 코드는 아래를 참조하십시오. 감사. --JR
package mypackage;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* Note: This class is only instantiated once per application run.
* Multiple instantiations, as specified in the release notes,
* are not supported.
*/
public class ConnectionManager {
// Use a blocking queue to store the database connections.
// The application will only be called once, by a single user,
// but within the application many threads will require
// a connection.
private BlockingQueue<Connection> connectionQueue = null;
// Load the connection queue with a user-defined number of connections.
// Params contains a map of all non hard-coded variables in the
// application.
public ConnectionManager(int howMany, Map<String, Object> params) {
Database database = new Database();
connectionQueue = new ArrayBlockingQueue<Connection>(howMany);
for(int i = 0; i < howMany; i++) {
connectionQueue.add(database.getConn(params));
}
}
// Return a connection from the queue, waiting up to 15 minutes to do so.
// 15 minutes is hard-coded because it is the standard time-out for all
// processes at our agency. This application must complete in less
// than fifteen minutes (is currently completing in thirty five seconds).
public Connection getConnection() {
Connection conn = null;
try {
conn = connectionQueue.poll(15, TimeUnit.MINUTES);
}
catch(InterruptedException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
// Returns a connection to the connection queue.
public void returnConnectionToManager(Connection conn) {
connectionQueue.add(conn);
}
// Called on the last line of the application program's dispatcher.
// Closes all active connections (which will only exist if there
// was a failure within one of the worker threads).
public void closeAllConnections() {
for(Connection conn : connectionQueue) {
try {
conn.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
}
호출 예 :
...
private ConnectionManager cm;
...
public Table(Map<String, Object> params, String method) {
...
cm = (ConnectionManager) params.get("cm");
}
// Execute a chunk of SQL code without requiring processing of a
// result set. Acquires connection from pool via cm.getConnection
// and releases connection via cm.returnConnectionToManager.
// (Database is just a helper class with simple methods for
// closing prepared statement, result sets, etc.)
private void execute(String sql) {
PreparedStatement ps = null;
Connection conn = null;
try {
conn = cm.getConnection();
ps = conn.prepareStatement(sql);
ps.execute();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
database.closePreparedStatement(ps);
cm.returnConnectionToManager(conn);
}
}
저장 싱글 톤 클래스 또는 열거에 객체입니다. Prefarably enum. 이 방법으로 연결 풀에 쉽게 액세스 할 수 있으며 여러 개체를 쉽게 피할 수 있습니다. DB 트랜잭션으로 응용 프로그램 코드가 끝나면 개별 연결을 해제하는 메소드를 추가합니다. – Acewin
'내가 일하는 곳에서는 보안 계층을 거치지 않고 API를 사용할 수 없습니다. '라는 소리가 나옵니다. 여기서 발명되지 않은 나쁜 경우와 같습니다. 수천 명의 사람들이 시도하고 테스트 한 오픈 소스 프레임 워크보다 사내 코드가 다소 안전하다고 생각하는 이유는 언제나 나를 놀라게합니다. –
API를 지우는 코드를 작성하는 것이 아니라 IMO를 사용하는 것이 더 좋을 것입니다. – Acewin