Java DBCP Configuration

Source: Internet
Author: User

SDF

Package Com. mengya. connutil;

ImportJava. Io. inputstream;
ImportJava. SQL. connection;
ImportJava. SQL. resultset;
ImportJava. SQL. sqlexception;
ImportJava. SQL. statement;
ImportJava. util. properties;

ImportJavax. SQL. datasource;

ImportOrg. Apache. commons. DBCP. basicdatasourcefactory;

Public Class Sqlconnutil {

Private Static Inputstream instream;
Private Static Properties pro;
Private Static Datasource;

Static {
Instream = sqlconnutil. Class . Getclass (). getresourceasstream ("/dbcpconfig. properties ");
Pro = New Properties ();
Try {
Pro. Load (instream );
Datasource = basicdatasourcefactory. createdatasource (Pro );
} Catch (Exception e ){
E. printstacktrace ();
Throw New Runtimeexception ("initialization error! ");
}
}

Public Static Datasource getdatasource (){
Return Datasource;
}

Public Static Connection getconnection (){
Try {
Return Datasource. getconnection ();
} Catch (Sqlexception e ){
E. printstacktrace ();
Throw New Runtimeexception ("failed to get database connection! ");
}
}

Public Static Void Free (resultset RS, statement STA, connection conn ){
Try {
If (RS! = Null ){
Rs. Close ();
}
} Catch (Exception e ){
E. printstacktrace ();
} Finally {
Try {
If (STA! = Null ){
Sta. Close ();
}
} Catch (Exception e ){
E. printstacktrace ();
} Finally {
If (Conn! = Null ){
Try {
Conn. Close ();
} Catch (Sqlexception e ){
E. printstacktrace ();
}
}
}
}
}
}

 

# Connection settings
Driverclassname = com. Microsoft. sqlserver. JDBC. sqlserverdriver
Url = JDBC: sqlserver: // localhost: 1433; databasename = MP
Username = sa
Password = mengya

#<! --Initialize connection-->
Initialsize = 10

# Maximum number of connections
Maxactive = 50

#<! --Maximum idle connections-->
Maxidle = 20

#<! --Minimum idle connection-->
Minidle = 5

#<! --Timeout wait time is measured in milliseconds. The unit is 6000 milliseconds/1000 is equal to 60 seconds.-->
Maxwait = 60000

# The format of the connection property that is attached to the JDBC driver when establishing a connection must be as follows: [property name = property;]
# Note: The attributes "user" and "password" are passed explicitly, so they are not included here.
Connectionproperties = useunicode = true; characterencoding = GBK

# Specify the auto-commit status of the connection created by the connection pool.
Defaultautocommit = true

# Driver default specifies the read-only status of the connection created by the connection pool.
# If this value is not set, the "setreadonly" method will not be called. (Some drivers do not support read-only mode, such as Informix)
Defaultreadonly =

# Driver default specifies the transaction level (transactionisolation) of the connection created by the connection pool ).
# The available value is one of the following: (for details, see javadoc .) None, read_uncommitted, read_committed, repeatable_read, serializable
Defaulttransactionisolation = read_uncommitted

SDF

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.