2013-04-06 2 views
0

저는 데이터베이스 작업을하고 있으며, 천 번 반복 한 것처럼 일부 항목을 선택합니다. 그러나 이번에는 resultSet.next()을 호출하면 프로그램이 정지되고 잠시 후에 예외가 발생합니다. 어떤 아이디어, 내가 실수 할 수 있니? 이 동안 사이클의 조건으로 호출 될 때 정확히 resultsDB.next()에서Java에서 ResultSet에서 next를 호출 할 때 시간 초과 예외가 발생했습니다.

try { 
     connection = dataSource.getConnection(); 
     connection.setAutoCommit(false); 
     query = connection.prepareStatement(
       "SELECT * FROM INGREDIENTS WHERE RECIPEID = ?"); 

     query.setLong(1, recipe.getId()); 

     ResultSet resultsDB = query.executeQuery(); 

     SortedSet<Ingredient> result = new TreeSet<Ingredient>(); 
     while (resultsDB.next()) { 
      Ingredient output = rowToIngredient(resultsDB); 
      validate(output); 

      result.add(output); 
     } 
     connection.commit(); 
     return result; 
    } 

프로그램이 정지 : 여기에 내 코드입니다. 이 코드 부분이 처음 실행되면 프로그램이 다른 데이터로 두 번째로 도달하면 .next() 메서드가 프로그램을 고정시킵니다. 프로그램은 절대로 사이클에 들어 가지 않고 중단 점을 사용하고 조건이 평가되는 행에서 실패합니다. 조언을 주셔서 감사합니다

이 전체 코드는 순환하고 있습니다. 괜찮습니까? 나는 데이터 소스를 사용하고 마찬가지로 지금까지, 그것은해야

오류 :

java.sql.SQLTransactionRollbackException: A lock could not be obtained within the time requested 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedResultSet.closeOnTransactionError(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedResultSet.movePosition(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedResultSet.next(Unknown Source) 
    at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207) 
    at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207) 
    at fi.muni.pv168.RecipebookImpl.getIngredientsOfRecipe(RecipebookImpl.java:53) 
    at fi.muni.pv168.RecipebookImpl.isIngredientInRecipe(RecipebookImpl.java:324) 
    at fi.muni.pv168.RecipebookImpl.addIngredientsToRecipe(RecipebookImpl.java:92) 
    at fi.muni.pv168.RecipebookImplTest.testFindRecipesByIngredients(RecipebookImplTest.java:390) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 
Caused by: java.sql.SQLException: A lock could not be obtained within the time requested 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) 
... 46 more 

하고있다 몇

+0

아니요, 중단 점을 시도했지만 프로그램이주기에 도달하지 못했습니다. 조건이 평가되는 행에서 실패합니다. –

+0

@orid 'hasNext' 메소드는 어디에서 찾을 수 있습니까? [나는 하나도 보이지 않습니다.] (http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html). – Dukeling

+0

해당 next()가 아니라 hasNext() –

답변

1
당신은 행을 업데이트하고 다른 동시 아닌 최선을 다하고 거래를해야합니다

당신을 읽으 려하고있어. 이로 인해 행이 잠기고이 잠금이 해제 될 때까지 트랜잭션이 대기합니다.

데이터베이스에서 요청을 실행하는 도구를 사용하는 경우이 도구에서 현재 트랜잭션을 커밋하거나 롤백해야합니다. 다른 응용 프로그램이나 동일한 응용 프로그램에 동일한 데이터베이스를 사용하는 다른 스레드가있는 경우 장기 실행 트랜잭션이 없는지 확인하십시오.

+0

나는이 문제에 대해 단계적으로 프로그램을 검토했으며 아무 것도 알아 내지 못했습니다. DB를 사용하고있는 것을 확인하는 다른 방법이 있습니까? 추신 난 단지 1 스레드가 –

+0

내가 뭘 잘못했는지 모르겠지만, 내 코드를 조금 재구성하고 지금은 작동합니다. 감사 –

관련 문제