SQLNonTransientConnectionException:在与Derby数据库交互时我的应用程序中没有当前连接

时间:2011-05-30 06:23:57

标签: database jdbc database-connection derby

我在我的应用程序中实现了derby数据库以保存我的数据,并且在Derby数据库中检索结果集形式select查询时遇到此异常。 要建立连接,我使用连接字符串:

我使用这种方法建立连接:

我在类 Connection.java

中声明了这个方法
 public synchronized Connection createConnection() throws Exception {

        Connection rescon = null;

        try {
            if (this.dbuser == null) {
                rescon = DriverManager.getConnection(this.URI + ";create=true");
                } else {
                rescon = DriverManager.getConnection(this.URI + ";create=true",
                        this.dbuser, this.dbpass);
            }
            // new connection in connection pool created
        } catch (SQLException e) {
            final String stackNum = Utility.exceptionHandler(e);
            throw new Exception("Exception get during Connection - " + e.getMessage());
        }
        return rescon;
    }

我使用此方法作为并创建连接,在使用string:

进行插入时创建连接
private Connection objConnection = null;
objConnectionpool = new Connection("jdbc:derby:" + Folder.getAbsolutePath() +        "/myapplication" + sFileName, null, null, "org.apache.derby.jdbc.EmbeddedDriver");
objConnection =  objConnectionpool.createNewConnection();

我在执行查询时遇到异常:

sQuery = "select value,reason from " + TableNm + " where fileName ='" + FileName + "' AND type='"+Type+"' AND status ='remaining'"; 

在处理和与Derby数据库交互期间,我们可能也会更新数据库中的数据:

我在插入数据库之前执行setAutoCommit为false

objConnection.setAutoCommit(false);
插入后

ps = objConnection.prepareStatement(sQuery);
rs = ps.executeQuery();
objConnection.commit();
objConnection.setAutoCommit(true);

我的异常是

java.sql.SQLNonTransientConnectionException: No current connection.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.noCurrentConnection(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.checkIfClosed(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.setupContextStack(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at com.myapplication.c.aw.a(Unknown Source)
    at com.myapplication.c.aw.a(Unknown Source)
    at com.myapplication.main.avd.run(Unknown Source)
Caused by: java.sql.SQLException: No current connection.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 11 more

2 个答案:

答案 0 :(得分:3)

通过Google发现此信息。

我遇到了同样的问题,并在此示例应用程序中找到了我的答案:http://db.apache.org/derby/docs/10.4/devguide/rdevcsecure26537.html

// force garbage collection to unload the EmbeddedDriver
// so Derby can be restarted
System.gc();

现在完美运作。

答案 1 :(得分:0)

什么是Connection.createNewConnection?我从未在java.sql.Connection上听说过这样的方法,我在任何文档中都找不到它。

我总是使用DriverManager.getConnection来建立我的Derby数据库连接。