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

发布于 2024-11-10 13:30:21 字数 2882 浏览 2 评论 0原文

我在应用程序中实现了 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;
    }

我将此方法用作并创建连接,在使用字符串的牵引期间创建连接:

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

I implemented derby data base in my application to save my data,and I am getting this exception in retrieval of result set form select query in Derby database.
To establish the connection I use connection string as:

I establish connection using this method:

I declare this method in class 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;
    }

I used this method as and create connection, create connection during intraction using string:

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

I am getting exception in execution of query:

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

during processing and interaction with Derby database we might update as well get the data from the database:

I perform setAutoCommit as false before inserting in the database

objConnection.setAutoCommit(false);

after insertion:

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

I am getting Exception as

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-11-17 13:30:21

通过谷歌找到这个。

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

//强制垃圾回收卸载EmbeddedDriver
// 这样 Derby 就可以重新开始
系统.gc();

现在工作完美。

Found this via Google.

I had the same problem and found my answer in this sample application: 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();

Works perfectly now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文