我不断收到此错误,我不确定为什么。这是代码。我什至没有执行任何SQL,它仍然在说“在事务仍处于活动状态时无法关闭连接”
DataAccess da = new DataAccess();
int param = Integer.parseInt(request.getParameter("geoAreaID"));
try (Connection tempConnection = DataAccess.OpenConnection())
{
tempConnection.rollback();
tempConnection.close();
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(e.getErrorCode());
}
catch (Exception e)
{
e.printStackTrace();
System.exit(2);
}
DataAccess类具有打开连接的方法,就在这里。
public static Connection OpenConnection() throws SQLException
{
Properties tempConnectionProperties = new Properties();
tempConnectionProperties.put("user", CONNECTION_USER);
tempConnectionProperties.put("password", CONNECTION_PASSWORD);
Connection tempConnection = DriverManager.getConnection(CONNECTION_STRING, tempConnectionProperties);
tempConnection.setAutoCommit(true);
tempConnection.createStatement().executeUpdate("SET SCHEMA APP");
return tempConnection;
}
这是完整的错误消息
java.sql.SQLException: Cannot close a connection while a transaction is still active.
at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.ClientConnection.closeResourcesX(Unknown Source)
at org.apache.derby.client.am.ClientConnection.closeX(Unknown Source)
at org.apache.derby.client.am.ClientConnection.close(Unknown Source)
at org.apache.derby.client.net.NetConnection.close(Unknown Source)
at dkaru.servlets.GeographicAreaServlet.doGet(GeographicAreaServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: ERROR 25001: Cannot close a connection while a transaction is still active.
at org.apache.derby.client.am.ClientConnection.checkForTransactionInProgress(Unknown Source)
... 29 more
答案 0 :(得分:0)
设置连接的自动提交属性对我有用。代码如下:
conn.setAutoCommit(true);