Use dbcp to configure the data source and dbcp Data Source

Source: Internet
Author: User

Use dbcp to configure the data source and dbcp Data Source
1. Configure the dbcp data source requires two jar packages, commons-dbcp-x.x.x.jar and commons-pool.x.x.x.jar, the version difference is not very big, of course also need database driver
2. You can use the configuration file or manually configure it directly in the program. (It is recommended that you use the configuration file more conveniently)
Manual configuration in the program:

Package com. silence. database;

Import java. SQL. Connection;
Import java. SQL. SQLException;
Import org. apache. commons. dbcp. BasicDataSource;
Import org. junit. Test;

Public class Test1 {
@ Test
Public void testDBCP () throws SQLException {
// Create a database connection pool
Final BasicDataSource dataSource = new BasicDataSource ();
// Specify the database username
DataSource. setUsername ("root ");
// Specify the Database Password
DataSource. setPassword ("root ");
// Specify the URL of the database
DataSource. setUrl ("jdbc: mysql: // localhost: 3306 ");
// Specify the driver name of the database
DataSource. setDriverClassName ("com. mysql. jdbc. Driver ");
// Specify the number of initial connections in the database connection pool
DataSource. setInitialSize (5 );
// Specify the maximum number of connections: the number of connections that can be applied to the database at the same time
DataSource. setMaxActive (5 );
// Specify the number of small connections: the minimum number of idle connections saved in the database connection pool
DataSource. setMinIdle (2 );
// The maximum wait time for the database connection pool to allocate connections. Unit: millisecond. An exception will be thrown if the connection exceeds this time.
DataSource. setMaxWait (1000*5 );
// Obtain the database connection from the data source
Connection connection = dataSource. getConnection ();
System. out. println (connection );
System. out. println (connection. getClass ());
}
}
The output is as follows:
Jdbc: mysql: // localhost: 3306, UserName = root @ localhost, MySQL-AB JDBC Driver
Class org. apache. commons. dbcp. PoolingDataSource $ PoolGuardConnectionWrapper


2. Create a connection pool using the configuration file
The content of the dbcpconfig. properties file is as follows: (this file is a complete configuration file found from the Network)
# Connection settings
Url = jdbc: mysql: // localhost: 3306/test
DriverClassName = com. mysql. jdbc. Driver
Username = root
Password = root
# Size of the initial connection
InitialSize = 10
# Maximum number of database connections in the connection pool. If it is set to 0, there is no limit.
MaxActive = 100
# Maximum idle connection of the Connection Pool
MaxIdle = 50
# Minimum idle connection of the Connection Pool
MinIdle = 5
# The timeout wait time is measured in milliseconds. The unit is 6000 milliseconds/1000 is equal to 60 seconds.
MaxWait = 80000
# 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 = utf8
# 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

# Oracle only supports READ_COMMITTED (default). The SERIALIZABLE program is as follows:

Package com. silence. database;

Import java. io. InputStream;
Import java. SQL. Connection;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. util. Properties;

Import javax. SQL. DataSource;

Import org. apache. commons. dbcp. BasicDataSourceFactory;

Public class Test {
Private static DataSource ds;
Static {
Try {
// Read the configuration file from the src directory
InputStream in = Test. class. getClassLoader (). getResourceAsStream ("dbcpconfig. properties ");
Properties props = new Properties ();
// Use properties to read configurations
Props. load (in );
// Obtain the data source
Ds = BasicDataSourceFactory. createDataSource (props );
Connection conn = ds. getConnection ();
System. out. println (conn );
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public static Connection getConnection (){
Try {
Return ds. getConnection ();
} Catch (SQLException e ){
Throw new RuntimeException (e );
}
}
Public static void main (String [] args ){
Connection conn = getConnection ();
System. out. println (conn );
System. out. println (conn. getClass ());
}
}}

Related Article

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.