Establishing a Connection
Creating and establishing a database connections are relatively very expensive. Because it involves processes like establishing a network connection, initializing database session, authorization in the back end database etc.Database or DBMS is a Data Source. A legacy file system or some other data source with a corresponding JDBC driver are also data sources.
When establishing a connection to a database, a JDBC (Java DataBase Client) Application can use any of the two classes
- Driver Manager
driver manager class calls the method DriverManager.getConnection to create the connection.The method requires a database URL depending on your DBMS. eg:
jdbc:mysql://localhost:3306/
- Data Source
Advantages of using Data Source over Driver Manager
02) enables the connection pooling and distributed transaction
mechanisms
03) properties of data source are kept in a configuration file,
so that any change to the data source or data drivers can
be made in the configuration file. But in driver manager
properties are hard coded in the application. So after the
change the code has to recompiled
C3P0 provides Data Source implementations
What is C3P0 ?
C3P0 is an easy-to-use library. Increase the functionality of traditional JDBC drivers and make them enterprise ready. The most useful service it provides is when acquiring database connections it provides newer javax.sql.DataSource scheme which has many advantages over the traditional DriverManager based JDBC drivers.Prerequisities
requires level 1.6.x or above Java Runtime environmentC3P0 Usage
For the users c3p0 provides standard jdbc DataSource objects. 3 ways are available to acquire c3p0 pool-backed datasources.- directly instantiate and configure a ComboPooledDataSource
- instantiate PoolBackedDataSource and set its ConectionPoolDataSource and
First method among the above mentioned 3 methods is regarded as the most convenient method. Lets discuss each in detail.
Method 1
Instantiating and Configuring a ComboPooledDataSource
- instantiate an instance of com.mchange.v2.c3p0.ComboPooledDataSource.
- ComboPooledDataSource() is a public ,no-arg constructor.
- But before using the DataSource, make sure to set at least the property
jdbcUrl.
- Following shows the code for fully configuring the DataSourceand make a
usable pooled DataSource.
- Above configured DataSource can then be used to make the connection
with the database as follows
ComboPooledDataSource dataSource = DatabaseUtility.getDataSource();
Connection connection = dataSource.getConnection();
- Furthermore, we can configure multiple DataSources using named
configurations supported by c3p0.
- For that, construct the ComboPooledDataSource with the configuration
name as the constructor argument as below.
Method 2
Using the DataSources factory class
hope detailed explanation on this method soonUsing any of the above mentioned methods we can create our Data Source.
For any Data Source built in, c3p0 use default values for the configuration parameters, if not programmed specifically. But these default values can be overridden using configuration files. Configuration also can be done in 3 ways.
- using a simple java.util.Properties files called c3p0.properties
- using a XML formatted file called c3p0-config.xml
- using more advanced HOCON configuration files called application.json
No comments:
Post a Comment