JDBC Connection Pool

Source: Internet
Author: User
Tags rollback

Import Java.io.InputStream;
Import java.sql.Connection;
Import java.util.Properties;

Import Org.apache.commons.dbcp.BasicDataSource;

public class Dbutils {
private static String driver;
private static String URL;
private static String user;
private static String password;
private static int initsize;
private static int maxactive;
private static Basicdatasource DS;

static{
Ds=new Basicdatasource ();
Properties Cfg=new properties ();
try {
InputStream In=dbutils.class.getclassloader ().
getResourceAsStream ("Db.properties");
Cfg.load (in);
Initialize parameters
Driver=cfg.getproperty ("Jdbc.driver");
Url=cfg.getproperty ("Jdbc.url");
User=cfg.getproperty ("Jdbc.user");
Password=cfg.getproperty ("Jdbc.password");
Initsize=integer.parseint (Cfg.getproperty ("initsize"));
Maxactive=integer.parseint (Cfg.getproperty ("maxactive"));
In.close ();
Initializing the connection pool
Ds.setdriverclassname (driver);
Ds.seturl (URL);
Ds.setusername (user);
Ds.setpassword (password);
Ds.setinitialsize (initsize);
Ds.setmaxactive (maxactive);

} catch (Exception e) {
E.printstacktrace ();
throw new RuntimeException ();
}
}

public static Connection getconnection () {
try {
Connection conn=ds.getconnection ();
Return conn;
} catch (Exception e) {
E.printstacktrace ();
throw new RuntimeException ();
}

}

public static void Close (Connection conn) {
if (conn!=null) {
try {
Conn.close ();
} catch (Exception e) {

}
}
}
public static void rollback (Connection conn) {
if (conn!=null) {
try {
Conn.rollback ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}

JDBC Connection Pool

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.