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

use*_*621 7 database database-connection jdbc derby

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

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

我在类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;
    }
Run Code Online (Sandbox Code Playgroud)

我使用此方法作为并创建连接,在使用字符串进行插入时创建连接:

private Connection objConnection = null;
objConnectionpool = new Connection("jdbc:derby:" + Folder.getAbsolutePath() +        "/myapplication" + sFileName, null, null, "org.apache.derby.jdbc.EmbeddedDriver");
objConnection =  objConnectionpool.createNewConnection();
Run Code Online (Sandbox Code Playgroud)

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

sQuery = "select value,reason from " + TableNm + " where fileName ='" + FileName + "' AND type='"+Type+"' AND status ='remaining'"; 
Run Code Online (Sandbox Code Playgroud)

在处理和与Derby数据库交互期间,我们可能会更新以及从数据库中获取数据:

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

objConnection.setAutoCommit(false);
Run Code Online (Sandbox Code Playgroud)

插入后:

ps = objConnection.prepareStatement(sQuery);
rs = ps.executeQuery();
objConnection.commit();
objConnection.setAutoCommit(true);
Run Code Online (Sandbox Code Playgroud)

我得到的是Exception

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
Run Code Online (Sandbox Code Playgroud)

Whi*_*red 5

通过谷歌找到了这个。

我遇到了同样的问题,并在此示例应用程序中找到了答案: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();
Run Code Online (Sandbox Code Playgroud)

现在完美运行。