Data Source Configuration

Source: Internet
Author: User

Go to:Spring get Started (vi) "Data source configuration in Springmvc" && Configure several common connection pools under spring

In the process of using spring for javaweb development, it is necessary to exchange data with the database, to get the database connection frequently, to obtain the database connection using JDBC, and then release the connection after use, this process consumes the system resources undoubtedly very much. Here is a brief description of the configuration of the three database connection pools, which can be used to obtain a data source. 1, the spring comes with the JDBC connection pool; 2, c3p0;3, DBCP;

First, the spring comes with the JDBC way

Spring provides support for JDBC and provides the configuration of a JDBC-based data source, such as the following configuration file

<bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >        < Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver" ></property>        <property name= "url" Value= "Jdbc:mysql://127.0.0.1:3306/test" ></property>        <property name= "username" value= "root" > </property>        <property name= "password" value= "123456" ></property></bean>

In the spring configuration file, configure the above bean to get a JDBC data source.

Second, c3p0

C3P0 is an open source database connection pool, which can be used to manage the data connection well, the following configuration





</bean>

In the spring configuration file, the above bean is configured to get a c3p0 data source;

Combopooleddatasource provides a close () method for shutting down the data source so that we can guarantee that the data source will be released successfully when the spring container is closed.


Note: To use the C3P0 connection pool, you must import the C3P0 jar package. Prior to version 0.9.1, C3P0 had only one or two jar packages: C3p0-0.9.1.jar, separated from the 0.9.2 after another package: Mchange-commons-java-0.2.11.jar, I used the 0.9.5.2 version here, so I imported two jar packages: C3 P0-0.9.5.2.jar, Mchange-commons-java-0.2.11.jar. The specific can be online query.

Other related configurations:

acquireincrement: When a connection in the connection pool is exhausted, C3P0 creates the number of new connections at once; Acquireretryattempts: Defines the number of times that a new connection fails to be fetched from the database, and the default is----; Acquirere Trydelay: The interval between two connections, in milliseconds, by default, Autocommitonclose: All uncommitted operations are rolled back by default when the connection is closed. The default is false; Automatictesttable:c3p0 will build an empty table named Test and test it with its own query statement. If this parameter is defined, then the property preferredtestquery is ignored. You cannot do anything on this test table, it will be used in the C3P0 test, the default is null, and breakafteracquirefailure: Getting a connection failure will cause all threads waiting to get the connection to throw an exception. However, the data source is still valid and continues to try to get the connection the next time you call Getconnection (). If set to True, the data source will declare broken and permanently shut down after attempting to acquire a connection failure. Default isfalseCheckouttimeout: When the connection pool is exhausted, the client calls getconnection () to wait for the new connection, and after the timeout, the SqlException is thrown and, if set to 0, waits indefinitely. Unit milliseconds, default 0, Connectiontesterclassname: To test a connection by implementing a class of Connectiontester or Queryconnectiontester, the class name needs to be set to the fully qualified name. The default is Com.mchange.v2.C3P0.impl.DefaultConnectionTester; Idleconnectiontestperiod: How many seconds to check for idle connections in all connection pools, default to 0 means no check; Initi Alpoolsize: The number of connections created at initialization should be taken between Minpoolsize and Maxpoolsize. The default is 3, MaxIdleTime: Maximum idle time, and connections that exceed idle time are discarded. 0 or negative numbers will never be discarded. The default is 0; Maxpoolsize: The maximum number of connections that are kept in the connection pool. The default is the standard parameter of MAXSTATEMENTS:JDBC, which controls the number of PreparedStatement loaded within the data source. However, because the pre-cached statement belong to a single connection instead of the entire connection pool. So setting this parameter takes many factors into account, and if both maxstatements and maxstatementsperconnection are 0, the cache is closed. The default is 0, maxstatementsperconnection: The maximum number of cache statement that a single connection in the connection pool has. The default is 0; Numhelperthreads:c3p0 is asynchronous, and slow JDBC operations are done by the help process. Extending these operations can effectively improve performance, and multiple operations are performed simultaneously through multithreading. The default is 3; Preferredtestquery: Defines the test statements that are executed by all connection tests. This parameter can significantly improve the test speed when using the connection test. The test table must exist at the time of the initial data source. Default is NULL, Propertycycle: The maximum number of seconds a user waits before modifying system configuration parameters. The default is testconnectiononcheckout: Please use it only when you need it because of high performance consumption. If set to true then in each ConneCtion submission of the time are officer to verify its effectiveness. We recommend using methods such as Idleconnectiontestperiod or automatictesttable to improve the performance of your connectivity tests. The default is false; Testconnectiononcheckin: If set to true then officer the validity of the connection while the connection is made. The default is False.

Third, DBCP

DBCP is also an open source data connection pool, configured as follows,

<bean id= "datasourcedbcp" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >                < Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver" ></property>        <property name= "url" Value= "Jdbc:mysql://127.0.0.1:3306/test" ></property>        <property name= "username" value= "root" > </property>        <property name= "password" value= "123456" ></property></bean>

In the spring configuration file, the above bean is configured to get a DBCP data source;
Note: To use the DBCP connection pool you need to import the appropriate jar package, here are two jar packages imported: Commons-dbcp-1.4.jar, Commons-pool-1.5.6.jar

The configuration of the three data sources above is the most basic configuration, and if you need to get a high performance data stream, additional configuration items are required to perform the research.

Data Source Configuration

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.