2011-05-16 3 views
0

시나리오를 피하는 것은 다음과 같이이다 : 는 JDBC 호출

for loop // runs say 200000 times 
{ 
    // here, i do a select from a database, fetching few rows which are expected to increase with every new iteration of for loop 
    // currently i am doing this select using simple JDBC call (using JDBC only is NOT a requirement) 

    // then i do some string matching stuff and then i either insert or update a particular row (in 95% cases i will insert) 
    // this insert or update is being done using Hibernate (using Hibernate over here is a requirement) 
} 

그래서 문제가 모든 루프에서, 나는 각각의 모든 이전에 삽입/업데이트 행을 고려해야한다. 이 요구 사항으로 인해 모든 루프마다 JDBC 호출을 수행해야합니다. 그리고이 JDBC 호출은 성능을 떨어 뜨리는 데 최대 시간이 걸립니다.

알고 싶습니다. 각 반복에서 JDBC 호출을 할 필요가없는 메소드를 사용하고 있지만, 여전히 이전 삽입/업데이트에있는 레코드를 포함하여 모든 레코드를 고려할 수 있습니까? 캐싱 또는 일부 메모리 내 데이터 구조 또는 그와 유사한 것은 무엇입니까?

for loop // runs say 2000 times 
{ 
    String query = pdi.selectAllPatients(patientInfo); 
    Statement st = conn.createStatement(); 
    ResultSet patientRs = st.executeQuery(query); 

    while (patientRs.hasNext()) 
    { 
     // some string ops 
    } 

    // Create session for DB No.2 
    Session sessionEmpi = sessionFactoryEmpi.getCurrentSession(); 
    sessionEmpi.beginTransaction(); 

    if(some condition) 
     patientDao.insertPatient(patientInfo, sessionEmpi); 
    else 
     patientDao.insertref(patientInfo.getref(), sessionEmpi); 

    conn.commit(); 
} 

public int insertPatient(PatientInfo input, Session session) throws SQLException { 

    try { 

     session.save(input.getPatient()); 
     session.flush(); 
     session.save(input.getref()); 
     session.getTransaction().commit(); 

     return 1; 

    } catch (Exception ex) { 
     session.getTransaction().rollback(); 
     ex.printStackTrace(); 
     return 0; 
    } 
} 
+0

Java에서 nhibernate를 사용하고 있습니까? – abalogh

+0

나는 nhibernate가 아닌 Hibernate를 사용하고있다. – Bhushan

+1

그런 다음 'nhibernate'에 태그를 지정하지 마십시오. 나는 그것을 제거했다. 구체적인 문제에 관해서는 연결 풀을 사용하고 있습니까? 풀링되지 않은 연결을 얻는 데 최소 200ms의 비용이 필요하며 풀링 된 연결은 기본적으로 아무 작업도 수행하지 않습니다. – BalusC

답변

0

이 SELECT 일치의 성과가 : 여기

코드인가? 귀하의 데이터가 아주 작은 경우가 아니라면, 모든 변경 사항을 메모리에 캐싱하는 것이 어려울 것입니다. 또한 SELECT를 일괄 처리하여 루프를 효과적으로 풀 수 있습니다.