I was responsible for a project in 09, there are several groups of P590 small machines to provide services, once, our small machine incredibly collective downtime, the database can not be served, all the application of nature can not provide services. When the database is restored, all of the application's database connections are invalid, and only one by one restarts are in effect. The problem of the database, followed by a number of minor failures, because the application database connection is invalid, only through the subsequent restart to provide services, resulting in the application and further lag. Therefore, the follow-up began to focus on database re-training.
The following scenario uses DBCP to connect to a local Oracle database. After launching the application, turn off the database and then turn the database on to see if the connection is working properly.
Package test.ffm83.commons.dbcp;
Import Org.apache.commons.dbcp.BasicDataSource;
Import Org.apache.commons.dbcp.BasicDataSourceFactory;
Import Org.apache.commons.lang.StringUtils;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Connection;
Import java.util.Properties;
/* Connection to Oracle database via DBCP, database exception (shutdown, outage, etc.) restored after successful database connection
* Implemented with version 1.4
* @author Fan Fangming
* */
public class Dbcperrorconnection {
privatestatic Basicdatasource dataSource = null;
Public Dbcperrorconnection () {
}
publicstatic void Init () {
if (DataSource! = null) {
try{
Datasource.close ();
}catch (Exception e) {
E.printstacktrace ();
}
Datasource= null;
}
try{
Propertiesp = new Properties ();
P.setproperty ("Driverclassname", "Oracle.jdbc.driver.OracleDriver");
P.setproperty ("url", "JDBC:ORACLE:THIN:@127.0.0.1:1522:ORCL");
P.setproperty ("Password", "sq");
P.setproperty ("username", "sq");
P.setproperty ("Maxactive", "6"); //
P.setproperty ("Maxidle", "3");
P.setproperty ("Maxwait", "10");
P.setproperty ("removeabandoned", "true");//removal of unused connections
P.setproperty ("Removeabandonedtimeout", "3");
P.setproperty ("logabandoned", "true");
P.setproperty ("Testonborrow", "true");
P.setproperty ("Testonreturn", "true");
P.setproperty ("Testwhileidle", "true");
P.setproperty ("Validationquery", "Select 1 from Dual");
Datasource= (Basicdatasource) basicdatasourcefactory
. CreateDataSource (P);
}catch (Exception e) {
E.printstacktrace ();
}
}
Publicstatic synchronized Connection getconnection () throws SQLException {
if (DataSource = = null) {
Init ();
}
Connectionconn = null;
if (DataSource! = null) {
Conn= datasource.getconnection ();
}
Returnconn;
}
publicstatic void Main (string[] args) throws Exception {
for (int i = 0; i <; i++) {
try{
Getconandnotclose (i+ 1); It's not a good habit to put the exception in the loop, just to simulate some special cases.
Thread.Sleep (8* 1000); Pause for a little longer, and stop the database.
}catch (Exception e) {
E.printstacktrace ();
}
}
}
This method uses a database connection but does not release to simulate a connection pool overflow condition
privatestatic void getconandnotclose (int id) throws Exception {
Connectioncon = null;
try{
Con= dbcperrorconnection.getconnection ();
Stringsql = "Select sysdate from Dual";
PREPAREDSTATEMENTPS = con.preparestatement (sql);
Resultsetrs = Ps.executequery ();
while (Rs.next ()) {
StringValue = rs.getstring ("Sysdate");
System.out.println (Stringutils.center (value+ "," + ID
+ "Database connection succeeded", 50, "-"));
}
}catch (Exception e) {
E.printstacktrace ();
}
Note that the database connection pool is not closed here and the actual code is not written like this.
}
}
This code is not normal code, in the actual work, do not write this.
Apache DBCP Connection Database exception re-connect