java - 在 Java 中调用 ResultSet 的 next 时出现超时异常

标签 java database resultset

我正在使用数据库,并且我正在从中选择一些东西,就像我做过数千次一样。但这一次,程序在调用 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;
     }

当在 while 循环中作为条件调用时,程序会在 resultsDB.next() 处完全卡住。当这部分代码第一次执行时,它起作用,当程序第二次使用另一个数据到达那里时,.next()方法卡住程序。该程序永远不会进入循环,我使用了断点,并且它在评估条件的行上失败。谢谢各位的建议

这整个代码都在for循环中,可以吗?就我使用的数据源而言,应该是

错误:

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

还有更多

最佳答案

您必须有另一个并发的未提交事务正在更新您尝试读取的行。这会导致该行被锁定,并且您的事务将等待该锁被释放。

如果您使用工具在数据库上执行请求,请确保在此工具中提交或回滚当前事务。如果您有另一个应用程序,或同一应用程序中的另一个线程使用同一数据库,请检查它是否没有长时间运行的事务。

关于java - 在 Java 中调用 ResultSet 的 next 时出现超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854917/

相关文章:

java - 为什么我在指向 ServletFileUpload 的 HttpServletRequest 上收到 NoClassDefFoundError?

mysql - 在mysql php中以特定格式显示消息

php - PHP/MySQL与数据库的连接

Java 结果集 : Does the DB only update if updateRow is called?

java - 一个 ResultSet/ResultSet.next() 中的多个语句返回 false

java - 在java中显示数据库表

java - Hadoop Java 错误 : Exception in thread "main" java. lang.NoClassDefFoundError: WordCount (wrong name: org/myorg/WordCount)

java - 为什么 Java 中的 volatile 不更新变量的值?

java - 如何使用代码将一个或多个包正确地 move 到java中的另一个包中

mysql - 如何找出 MySQL 的 root 密码