[Go] Java configuration data source several ways

Source: Internet
Author: User
Tags connection pooling

The data source is the database connection pool inside the concept, the connection pool refers to when the server starts, first establishes several connections, when the application needs to connect with the database, obtains from the connection pool, after uses, does not connect the connection to break off, but puts back in the pool, thus reduces the data connection creation frequency, greatly enhances the connection performance.

Data source is to give the server a configuration information, and then the server will know how to use the JDBC driver, such as URL parameters, database instance name, user name and password, and so on.

The data source in Java is Javax.sql.DataSource. The creation of DataSource can have different implementations, following MySQL as an example to introduce several common datasource creation methods:

First, Jndi mode to create DataSource

To create a data source in Jndi, you first configure the associated connection information for the data source, which is the data source connection pool. This configuration should be configured in the Conf/context.xml file in the Tomcat installation directory, and the context.xml file can also be created under the/meta-inf directory under the Java EE schema of Eclipse. It is configured as follows:

<Context>    <Resourcename= "Jdbc/movie"Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "+"maxwait= "10000"username= "[user name]"Password= "[Password]"Driverclassname= "Com.mysql.jdbc.Driver"URL= "jdbc:mysql://localhost:3306/[Instance name]?autoreconnect=true" /></Context>

After the correct configuration, the data source can be created in Jndi in the program, get the database connection and do the corresponding operation. The code is as follows

Try{Context Context=NewInitialContext (); if(Context = =NULL){        Throw NewException ("Create Context failed!"); } DataSource DS= (DataSource) context.lookup ("java:comp/env/jdbc/[Instance name]"); if(ds = =NULL) {Thread.Sleep (2000); DS= (DataSource) context.lookup ("java:comp/env/jdbc/[Instance name]"); if(ds = =NULL) {             Throw NewException ("Get DataSource failed!"); }    }} Catch(namingexception ne) {ThrowNE;} Catch(Exception e) {Throwe;}

Second, Apache provides a simple connection pool to create a data source

To create a data source in this way, you must first prepare two jar files: Commons-dbcp.jar and Commons-pool.jar, and place the two jar packages in the Web-inf/lib directory. Data sources created in this way are no longer javax.sql.DataSource, but Org.apache.commons.dbcp.BasicDataSource. And you no longer need to configure any files to be used directly. The code is as follows:

Create Basicdatasource object    basicdatasource ds = new Basicdatasource ();    Ds.setdriverclassname ("Com.mysql.jdbc.Driver");    Ds.seturl ("jdbc:mysql://localhost:3306/[instance Name]");    Ds.setusername ("[user name]");    Ds.setpassword ("[Password]");    Ds.setinitialsize (+);    Ds.setmaxactive (+);    Ds.setmaxidle (+);    Ds.setmaxwait (10000);//Close data source connection    Ds.close ()

Third, C3P0 way to create a data source
Creating a data source using C3P0 should first prepare a jar file: C3p0-0.9.1.2.jar, put it in the Web-inf/lib directory, you can use C3P0 to create the data source in your project, C3P0 create the data source object is not a DataSource object, Instead of Combopooleddatasource, the code is as follows:

Create Combopooleddatasource object Combopooleddatasource ds = new Combopooleddatasource ();d S.setdriverclass (" Com.mysql.jdbc.Driver ");d S.setjdbcurl (" jdbc:mysql://localhost:3306/[instance Name] ");d s.setuser (" [user name] ");d S.setpassword ( "[Password]");d s.setinitialpoolsize;d s.setmaxpoolsize;d s.setmaxidletime (10000);

Iv. Proxool Way to create a data source
To create a data source in this way, you need to prepare the jar package: Proxool-01.9.0rc3.jar, put it in the Web-inf/lib directory, and then create the Proxooldatasource object in your project, with the following code:

Create Proxooldatasource object Proxooldatasource ds = new Proxooldatasource ();d s.setdriver ("Com.mysql.jdbc.Driver"); Ds.setdriverurl ("jdbc:mysql://localhost:3306/[instance Name]");d s.setuser ("[user name]");d s.setpassword ("[Password]");

V. BONECP way to create a data source

BONECP is a fast, efficient, open source, free Java database pool. The creators say the BONECP will outperform all major Java connection pools in performance. It can help you manage your data connections, allowing your application to access the database more quickly. 25 times times faster than C3P0/DBCP (databaseconnection pool, database connection pooling) connection pool. This database connection pool uses Google collection as the internal collection class framework, and the current version is already stable. To use BONECP, the jar files that must be used are:
· Bonecp-0.6.5.jar
· Google-collections-1.0.jar
· Slf4j-api-1.5.11.jar
· Slf4j-log4j12-1.5.11.jar
· Log4j-1.2.15.jar
By placing these jar packages in the Web-inf/lib directory, you can create Bonecpdatasource objects in your program, with the following code:

Create Bonecpdatasource object    bonecpdatasource ds = new Bonecpdatasource ();    Ds.setdriverclass ("Com.mysql.jdbc.Driver");    Ds.setjdbcurl ("jdbc:mysql://localhost:3306/[instance Name]");    Ds.setusername ("[user name]");    Ds.setpassword ("[Password]");    Ds.setacquireincrement (1);    Ds.setacquireretrydelay (10000);    Ds.setidleconnectiontestperiod (+);    Ds.setminconnectionsperpartition (2);    Ds.setmaxconnectionsperpartition (a);    Ds.setpartitioncount (2);

After you have created the data source, you can use JDBC to establish a connection between the program and the database, but be aware that there are related JDBC driver packages that require different drivers for different databases, which are generally available in the official network of each database.

[Go] Java configuration data source several ways

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.