Apache DBCP Connection Database exception re-connect

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.