这是本人项目里查询数据库方法里其中之一,之前用Oracle数据库时没有异常,
public DocumentImpl selectRecordById(DocumentImpl doc){ String name=null; String val=null; NodeList children = doc.getDocumentElement().getChildNodes(); name=children.item(0).getNodeName(); val=children.item(0).getTextContent(); //查询 Connection con=null; DocumentImpl result=new DocumentImpl(); Element root = result.createElement("root"); result.appendChild(root); try{ con=JdbcUtil.getConnection(); con.setAutoCommit(false); String sql="select * from clerk_ledger_xjy where "+name+" = ?"; //System.out.println(sql); PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, val); con.commit(); ResultSet rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); int count = rsmd.getColumnCount(); int row=1; while(rs.next()){ Element rows = result.createElement("row"+row); root.appendChild(rows); for (int i = 1;i <= count;i++){ String key = rsmd.getColumnLabel(i).toLowerCase(); String value = (rs.getString(key)==null?"":rs.getString(key)); WSDomUtil.appendChildNode(result , rows, key, value); //System.out.println(key+":"+value); } row++; } }catch (Exception e) { e.printStackTrace(); try { con.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } throw new RuntimeException( "查询失败", e); } finally { JdbcUtil.close(con); } // try { // System.out.println(WSDomUtil.convertXMLDOMToString(result)); // } catch (Exception e) { // e.printStackTrace(); // } return result; }
现在换成数据库db2,方法调用的时候老是报错:
com.ibm.db2.jcc.am.SqlException: [jcc][t4][10251][10308][3.57.82] 正在连接上执行事务时,请求了 java.sql.Connection.close()。 事务仍然保持活动状态,不能关闭连接。 ERRORCODE=-4471, SQLSTATE=null com.ibm.db2.jcc.am.bd.a(bd.java:660) com.ibm.db2.jcc.am.bd.a(bd.java:60) com.ibm.db2.jcc.am.bd.a(bd.java:120) com.ibm.db2.jcc.am.jb.u(jb.java:1167) com.ibm.db2.jcc.am.jb.x(jb.java:1190) com.ibm.db2.jcc.am.jb.v(jb.java:1176) com.ibm.db2.jcc.am.jb.close(jb.java:1160) util.JdbcUtil.close(JdbcUtil.java:41) dao.SelectDao.selectById(SelectDao.java:63) service.QueryServiceImpl.finduser(QueryServiceImpl.java:18) web.QueryController.query(QueryController.java:55) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844) javax.servlet.http.HttpServlet.service(HttpServlet.java:624) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
上面已经commit过,事务已经提交 ,下面关闭连接怎么还会出问题 ,求高手指点
解决方案
30
ps.executeQuery();//执行完后
con.commit();
con.commit();