我得到了非常模糊的异常connection exception : connection does not exist
,并花了我很多时间来找到它的来源。我一直在搜索HSQLDB文档及其邮件列表,stackoverflow等。最近我发现了异常的实际原因。
在我的jdbc项目中,我希望从HSQLDB获取自动生成的invoiceID
,但是当我尝试这样做时结果没问题,但之后当我运行另一个查询或更新数据库中的内容时得到这个例外。我已经观察了这个问题三个星期了。现在我发现getInvoiceID()
方法导致了它。当我评论这个方法时,项目运行良好但是在任何时候调用此函数都会阻止对jdbc的进一步调用。
我尝试使用其他方法从发票表中检索自动生成的字段,但问题仍然存在。
public static int getInvoiceID() {
int invoiceID = -1;
try ( Statement stmt = DatabaseManager.getInstance().getConnection()
.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet res = stmt.executeQuery("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_SESSIONS");) {
if(res.next())
invoiceID = res.getInt(1);
else
invoiceID = 1;
} catch (SQLException e) {
System.err.println("getInvoiceID Exception: " + e.getMessage());
}
return invoiceID;
}
上述解决方案是从Here
引用的public static int getInvoiceID() {
int invoiceID = -1;
try ( Statement stmt = DatabaseManager.getInstance().getConnection()
.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
stmt.executeQuery("INSERT INTO Invoice VALUES (NULL, NULL, NOW(), NULL);");
try (ResultSet result = stmt.executeQuery("CALL IDENTITY();")) {
if(result.next())
invoiceID = result.getInt(1);
} catch( SQLException e){
e.printStackTrace();
}
} catch (SQLException e) {
System.err.println("getInvoiceID Exception: " + e.getMessage());
}
return invoiceID;
}
我也尝试了generated keys
的解决方案昨天当我即将发布这个问题时,我发现异常被抛出,因为我使用ResultSet.TYPE_FORWARD_ONLY
而我改为ResultSet.TYPE_SCROLL_INSENSITIVE
,最终解决了这个问题。但是现在我非常确定这个异常是由方法getInvoiceID()
引起的,并且是由尝试访问数据库的下一个函数调用引发的。
这是我的表
CREATE TABLE IF NOT EXISTS Invoice(
InvoiceID INT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY,
ClientID VARCHAR(4),
Entry DATETIME NOT NULL,
TotalAmount DECIMAL(10,2) DEFAULT 0,
FOREIGN KEY (ClientID) REFERENCES Client(ClientID)
);
例外:
java.sql.SQLNonTransientConnectionException: connection exception: connection does not exist
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.connectionClosedException(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.checkClosed(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at database.ProductManager.getProductByID(ProductManager.java:203)
at gui.InvoicePanel$4.actionPerformed(InvoicePanel.java:423)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hsqldb.HsqlException: connection exception: connection does not exist
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 42 more
答案 0 :(得分:0)
问题在于Connection
我修改了我的代码并在执行任何SQL后关闭了connection
。这解决了我的问题如下:
// getConnection() returns new connection if null
DatabaseManager.getInstance().getConnection();
/*
* SQL
*/
DatabaseManager.getInstance().close();