5. Connect the data source DataSource to the database
The DataSource interface is newly added to the JDBC 2.0 API. It provides another method to connect to the data source. Using the DataSource object is the preferred method to connect to the data source.
This factory is used to provide a connection to the physical data source represented by this DataSource object. As an alternative to the DriverManager facility, the DataSource object is the preferred method for obtaining connections. Objects that implement the DataSource Interface are generally registered in the Naming Service Based on the JavaTM Naming and Directory Interface (JNDI) API.
The DataSource interface is implemented by the driver supplier. There are three types of implementations:
A) basic implementation-generate a standard Connection object
B) Connection pool implementation-generate a Connection object that automatically participates in the Connection pool. This implementation is used with the connection pool manager of the intermediate layer.
C) Distributed Transaction implementation-generate a Connection object that can be used for distributed transactions and is almost always involved in the Connection pool. This implementation is used with the Transaction Manager in the middle layer and almost always used with the connection pool manager.
The attribute of the DataSource object can be modified as needed. For example, if you move a data source to another server, you can change the server-related attributes. The advantage is that any code accessing the data source does not need to be changed because the attribute of the data source can be changed.
General steps:
5.1 import two class packages: commons-dbcp and commons-pool (from apache)
5.2 create a data source BasicDataSource ds = new BasicDataSource ();
5.3 set the basic information of the database connection pool, such:
Database driver, database connection protocol address, database user name, password
Maximum active connections, maximum idle connections, pre-compilation, etc.
5.4 activate Connection java. SQL. Connection conn = ds. getConnection ();
Key code:
BasicDataSource ds = new BasicDataSource ();
Ds. setDriverClassName (driverClassName );
Ds. setUrl (url );
Ds. setUsername (username );
Ds. setPassword (password );
/**
* Maximum number of active connections
* The number of concurrent database connections cannot exceed this value.
**/
Ds. setMaxActive (20 );
/**
* Maximum number of idle connections
* When a database connection is released and the number of idle connections exceeds this limit, some idle connections are closed.
*/
Ds. setMaxIdle (8 );
/**
* Whether to pre-compile SQL statements
**/
Ds. setPoolPreparedStatements (true );
Conn = ds. getConnection ();
Note: org. apache. commons. dbcp. basicDataSource does not support the getConnection (String username, String password) method. This class overrides javax. SQL. the method body directly throws the UnsupportedOperationException In the DataSource class. The Code is as follows:
Public Connection getConnection (String user, String pass) throws SQLException {
// This method isn't supported by the PoolingDataSource returned
// The createDataSource
Throw new UnsupportedOperationException ("Not supported by BasicDataSource ");
// Return createDataSource (). getConnection (username, password );