The project has been abnormal for some time, resulting in the inability to log on to the system, the database cannot connect
The exception information is as follows:
Java.sql.SQLException:Io Exception: Connection reset by Peer:socket write error at ORACLE.JDBC.DBACCESS.DBERROR.THROWSQL Exception (Dberror.java:134) at Oracle.jdbc.dbaccess.DBError.throwSqlException (Dberror.java:179) at Oracle.jdbc.dbaccess.DBError.throwSqlException (Dberror.java:333) at Oracle.jdbc.driver.OracleConnection.rollback (Oracleconnection.java:1380) at Com.newland.So.Transaction.executeQuery (Transaction.java:107) at Com.newland.Dao.UserInfoDao.findUser (Userinfodao.java:184) at Com.newland.Blo.UserInfoBlo.findUser (Userinfoblo.java:119) at Org.apache.jsp.loginaction_jsp._jspservice (Loginaction_jsp.java:111) at Org.apache.jasper.runtime.HttpJspBase.service (Httpjspbase.java:70) at Javax.servlet.http.HttpServlet.service (Httpservlet.java:723) at Org.apache.jasper.servlet.JspServletWrapper.service (Jspservletwrapper.java:388) at Org.apache.jasper.servlet.JspServlet.serviceJspFile (Jspservlet.java:313) at Org.apache.jasper.servlet.JspServlet.service (Jspservlet.java:260) at Javax.servlet.http.HttpServlet.service (Httpservlet.java:723) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (Applicationfilterchain.java:290) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (Applicationfilterchain.java:206) at Com.newland.So.EncodeFilter.doFilter (Encodefilter.java:32) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (Applicationfilterchain.java:235) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (Applicationfilterchain.java:206) at Org.apache.catalina.core.StandardWrapperValve.invoke (Standardwrappervalve.java:233) at Org.apache.catalina.core.StandardContextValve.invoke (Standardcontextvalve.java:191) at Org.apache.catalina.core.StandardHostValve.invoke (Standardhostvalve.java:127) at Org.apache.catalina.valves.ErrorReportValve.invoke (Errorreportvalve.java:103) at Org.apache.catalina.core.StandardEngineValve.invoke (Standardenginevalve.java:109) at Org.apache.catalina.connector.CoyoteAdapter.service (Coyoteadapter.java:293) at Org.apache.coyote.http11.Http11Processor.process (Http11processor.java:861) at Org.apache.coyote.http11.http11protocol$http11connectionhandler.process (Http11protocol.java:620) at Org.apache.tomcat.util.net.jioendpoint$worker.run (Jioendpoint.java:489) at Java.lang.Thread.run (Thread.java:744)
Resolution: When the above exception occurs because Tomcat's conf/context.xml is not joined to the database disconnection configuration, the configuration is as follows (the Scarlet Letter section):
<Context> <!--Default set of monitored resources - <Watchedresource>Web-inf/web.xml</Watchedresource> <!--uncomment this to disable session persistence across Tomcat restarts - <!--<manager pathname= ""/> - <!--uncomment this to enable Comet connection tacking (provides events in session expiration as well as WebApp Li Fecycle) - <!--<valve classname= "Org.apache.catalina.valves.CometConnectionManagerValve"/> - <!--Join break Line reconnection config - <spanstyle= "color: #ff0000;"><parameter> <name>Validationquery</name> <value>SELECT COUNT (*) from DUAL</value> </parameter> <parameter> <name>Testonborrow</name> <value>True</value> </parameter> <parameter> <name>Testonreturn</name> <value>True</value> </parameter> <parameter> <name>Testwhileidle</name> <value>True</value> </parameter></span></Context>
Configuration complete reboot or error, what to do, consider the connection pool to get a connection to replace the original JDBC connection, I chose the c3p0 below is some key code:
New Combopooleddatasource ();d atasource.setuser (connusername);d Atasource.setpassword (Connpassword); Datasource.setjdbcurl (Connurl); Datasource.setdriverclass ("Oracle.jdbc.driver.OracleDriver");d atasource.setinitialpoolsize ( initialpoolsize);d atasource.setminpoolsize (minpoolsize);d atasource.setmaxpoolsize (maxpoolsize); Datasource.setmaxstatements (maxstatements);d atasource.setmaxidletime (maxidletime);
Connection Pool if (datasource!=null) { conn = datasource.getconnection (); }
In this way to temporarily solve the problem, at least will not be logged on the system and then a few days and then there are problems, login can not, re-think, set the connection pool configuration, not reliable, because the IO is broken, re-connected problems, or self-change program bar to add abnormal automatic reconnection, The following is the InitDataSource key code, the simple notation of other database connections is not written:
// if it is a reconnection, get the connection to the connection pool Try { = datasource.getconnection (); Conn.setautocommit (false); Catch (SQLException e) { e.printstacktrace (); Reconnect (); // Auto-re-connect }
/**If the initialization database fails unlimited reconnection until the connection is successful * in order to resolve the exception: Java.sql.SQLException:Io exception: Connection reset by Peer:socket write error */ Public Static voidReconnect () { while(true) { if(conn = =NULL) {Logger.info ("-----------------start reinit dataSource-----------------"); Transaction.initdatasource (); } Else { Break; } } }
/*** Query data, conn is empty automatically re-connect *@paramQstr *@return * @throwsSQLException*/ PublicResultSet executeQuery (String qstr)throwssqlexception{ResultSet Cresultset=NULL; Try{ if(conn==NULL) {reconnect (); } stmt=conn.createstatement (); Cresultset=stmt.executequery (QSTR); } Catch(SQLException e) {stmt.close (); Conn.rollback (); Throwe; } returnCresultset; } /*** Data UPDATE, conn is empty automatically re-connect *@paramQstr *@throwsSQLException*/ Public voidExecuteupdate (String qstr)throwssqlexception{Try{ if(conn==NULL) {reconnect (); } stmt=conn.createstatement (); Stmt.executeupdate (QSTR); Stmt.close (); } Catch(SQLException e) {stmt.close (); Conn.rollback (); Throwe; } }
The system login module exception injects the re-connect mechanism:
Catch (SQLException e) { e.printstacktrace (); Transaction.initdatasource ();}
I don't know if anyone else has a better way to solve this kind of problem.
Old project with tomcat exception: Connection reset by Peer:socket write error exception resolution