C3p0 database connection pool dbutil

Source: Internet
Author: User
Package CN. secondteam. utils; import Java. beans. propertyvetoexception; import Java. SQL. connection; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import COM. mchange. v2.c3p0. combopooleddatasource; public class dbutil {final static string host = "127.0.0.1"; Final Static string name = "root"; Final Static string psw = "root"; static final St Ring dbname = "bbsdb"; Private Static connection; public static combopooleddatasource dswebgame; static {dswebgame = new combopooleddatasource (); try {dswebgame. setdriverclass (driver);} catch (propertyvetoexception e) {e. printstacktrace ();} // The parameter is read by the config class according to the configuration file. setjdbcurl (URL); // set the database logon username dswebgame. setuser (username); // set the password of the database logon user dswebgame. setpassword (password );// Set the maximum number of connections in the connection pool dswebgame. setmaxpoolsize (35); // set the minimum number of connections in the connection pool dswebgame. setminpoolsize (20); // sometimes a deadlock occurs. Set maxstatements to 0 dswebgame. setmaxstatements (0); // wait timeout smaller than MySQL. Dswebgame. setidleconnectiontestperiod (120);}/*** get database connection ** @ return */public static connection getconnection () {If (null = connection) {connection = newconnection ();} Try {If (connection. isclosed () {connection = dswebgame. getconnection () ;}} catch (sqlexception e) {e. printstacktrace ();} return connection;}/*** create a database connection * @ return */Private Static connection newconnection () {try {return (Conn Ection) dswebgame. getconnection ();} catch (sqlexception e) {e. printstacktrace (); return NULL ;}} public static void main (string ARGs []) {getconnection ();} /*** release resources ** @ Param conn * @ Param st * @ Param RS */public static void release (connection con, statement St, resultset RS) {If (RS! = NULL) {try {Rs. Close () ;}catch (exception e) {e. printstacktrace () ;}rs = NULL ;}if (st! = NULL) {try {st. Close () ;}catch (exception e) {e. printstacktrace () ;}} if (con! = NULL) {try {con. Close () ;}catch (exception e) {e. printstacktrace ();}}}}

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.