및 RecursiveAction
클래스를 사용하여 매우 큰 DB 테이블 (30kk 행 이상)의로드 데이터에 대해 프로그램의 다중 스레딩 부분 (GUI 사용)을 만들려는 경우가 많습니다. 실험적으로 확인했습니다. 예를 들어 이 같은 필요에 1000 뭔가에서 모든 포크로드 50 행 :JDBC 연결 동시성 충분
class ForkLoader extends RecursiveAction{
private static Connection con;
private Map<Integer, Double> map; //In our case ConcurrentHashMap
private final static int seqThreshold=50;
private List<Integer> id; //List with id_field in DB table
int start,end;
{
try {
con=DriverManager.getConnection("jdbc:mysql://some_ip/some_db","username", "password");
} catch (SQLException e) {
e.printStackTrace();
}
}
public ForkLoader(Map<Integer, Double> map, List<Integer> id,int start, int end){
this.map=map;
this.start=start;
this.end=end;
this.id=id;
}
@Override
protected void compute() {
if((end-start)<seqThreshold){
try {
Statement stmt=con.createStatement();
for(int i=start;i<end;i++){
String query="text of some query" + id.get(i);
ResultSet rs=stmt.executeQuery(query);
map.put(id.get(i), /*some double form result set*/);
}
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
int middle=(start+end)/2;
invokeAll(new ForkLoader(map, id, start, middle), new ForkLoader(map, id, middle,end));
}
}
}
는 안전 사용 모든 포크 하나 static
연결을 스레드인가? 이 작업을 해결하는 더 좋은 방법을 알고 있다면
이전 dbcp보다 훨씬 진보 된 apache의 jdbc-pool (tomcat 프로젝트의 일부)을 사용하는 것이 좋습니다. – MRalwasser
프로그램은 서블릿/애플릿이 아닌 독립 실행 형입니다. – disable1992
disable1992 OpenEJB 독립 실행 형을 사용할 수 있으며 웹 컨테이너가 필요하지 않습니다. –