2014-11-20 1 views
0

내가 만든 응용 프로그램을 통해 풀링 된 데이터 소스 (msaccess 데이터베이스)를 사용하여 로컬 데이터베이스 (h2 데이터베이스를 사용하는 클라이언트 측)를 업데이트하고 있습니다. 내가 가진 문제는 요청을 제출할 때 "INSERT INTO USERS (NAME, CODE) VALUES (Me, hfd5255fd4);" 응용 프로그램이 완벽하게 실행되고 아무 것도 오류 로그에보고되지만 데이터베이스에서 변경된 사항은 없습니다. c3p0 CombopooledDataSource가 SQL 업데이트를 수행하지 않음

private static Connection getDatabase() throws Exception { 


    cpds.setDriverClass("net.ucanaccess.jdbc.UcanaccessDriver"); 
    // loads the jdbc driver 
    cpds.setJdbcUrl("jdbc:ucanaccess://" 
      + (new File("Ressources/filter.mdb").getAbsolutePath())); 
    cpds.setUser("admin"); 
    cpds.setPassword("ibnsina"); 
    cpds.setAutoCommitOnClose(false); 
return cpds.getConnection(); //tried removing this , but no effect 
} 
----doing some other stuff--- 
private static updating() throws exception{ 
conn = getDatabase(); 

    File fileUpload = new File(logPath + "UploadLog.txt"); 
    BufferedReader readerUpload = new BufferedReader(new FileReader(
      fileUpload)); 
    String Uploadingline = ""; 
    StringBuffer secondaryline = new StringBuffer(); 
    if (readerUpload.ready()) { 
     System.out.println("Uploadtxt ready"); 
     Statement stUpload = conn.createStatement(); 
     System.out.println("Stupload ready"); 
     while ((Uploadingline = readerUpload.readLine()) != null) { 

      if (Uploadingline.endsWith(";")) { 
       secondaryline.append(Uploadingline); 
       /*stUpload.executeUpdate(secondaryline.toString()); tried this to execute each line separatly*/ 
       stUpload.addBatch(secondaryline.toString()); 
       System.out.println("Reading line :" + secondaryline); 
       secondaryline.setLength(0); 

      } else { 
       secondaryline.append(Uploadingline); 

      } 

     } 
     stUpload.executeBatch(); 
     stUpload.clearBatch(); 
     conn.commit(); //i even tried adding this to make it commit even tho autocommit is by default ON 
     stUpload.close();} 

답변

2

을 다음과 같이 내가 사용하고있는 코드는 각 연결에 대한 새로운 DataSource를 작성해서는 안, 당신은 단지 하나의 DataSource을 만들고 Connection의를 얻을 것을 사용해야합니다. close()을 기억하면 연결이 풀로 반환됩니다.

당신이 좋아하는 일을 수행해야합니다

// There should only ever be one of these. 
private static final DataSource ds = makeDataSource(); 

private static DataSource makeDataSource() { 
    ComboPooledDataSource cpds = new ComboPooledDataSource(); 
    cpds.setDriverClass("net.ucanaccess.jdbc.UcanaccessDriver"); 
    // loads the jdbc driver 
    cpds.setJdbcUrl("jdbc:ucanaccess://" 
      + (new File("Ressources/filter.mdb").getAbsolutePath())); 
    cpds.setUser("admin"); 
    cpds.setPassword("ibnsina"); 
    cpds.setAutoCommitOnClose(false); 
    return cpds; 
} 

private static Connection getConnection() { 
    return ds.getConnection(); 
} 

private static void releaseConnection (Connection conn) { 
    conn.commit(); 
    conn.close(); 
} 

private static void updating() { 
    Connection conn = getConnection(); 
    try { 
     //... 
    } finally { 
     releaseConnection(conn); 
    } 
} 
+1

이 무엇인지 포스터 이벤트보다 훨씬 더 많이. 사소한 단점 : 마지막으로 호출 된 releaseConnection() 메소드에는 conn.commit()이 포함되어서는 안됩니다. 그것은 두 가지 이유 때문에 중요합니다 : 1) 당신이 update()를 호출하는 메소드의 본문에서 Exception이 발생하면, 작업은 부분적으로 만 수행되고, commit (commit 절에서)를 호출하기보다는 rollback()(); 2) conn.commit() 중에 Exception이 발생하면 conn.close()에 대한 호출이 건너 뜁니다. 이로 인해 Connections가 풀로 반환되지 않을 수 있습니다. –

+1

트랜잭션 동작을 원할 경우 try 블록 맨 위에서 conn.setAutoCommit (false)를 호출하고 작업 한 다음 try 블록의 맨 아래에있는 conn.commit()을 호출하십시오. conn.rollback()을 catch 절에 넣고 conn.close()를 마지막에 넣습니다. –

+0

'throws Exception'을 추가 한 후에'return cpds' 유형이 Datasource 대신 ComboPooledDatasource가되어서는 안됩니까 ?? 컴파일 오류가 발생합니다. @ Steve Waldman : 일방적 인 트랜잭션이기 때문에 conn.rollback()이 필요하지 않지만 나중에 포함하고 싶다면 rollback()을 호출하면 이전에 수행 된 모든 변경 사항이 취소되지 않습니다. stUpload.executebatch()가 호출 되었습니까? –

관련 문제