C3P0 Configuration in Spring

Source: Internet
Author: User

In everyone's development and learning which should be used frequently to database connection and use, just connect

In many ways, such as using the most simple JDBC, and more practical than

A more complex point is the database connection pool. Of course there are connections using DBCP, and various parties

Method has the advantages and disadvantages of various methods, it is necessary for us to analyze the details of

, for example, if a project is not very large, and the nature of the hardware machine used

Could not be very good. The database connection pool is not expected to be used, after all, the connection

Ikenotaira Onsen always manages the connection. Will greatly occupy the performance and efficiency of the machine.

(The use of c3p0 here is based on the use of Spring framework development, and the IDE tools are

"MyEclipse 9")

Here we describe how to use the C3P0 database connection pool in project, which is a

An open source connection pool notation that has been written and highly efficient. The number of parameters is set

are simpler and more reasonable.

The first step is to build a project that will need to use C3P0. Of course, the simplest is also necessary

is to bring in the required jar package: So much less nutritious, I won't post

Out, decisive Baidu's bar, then of course, is "BuildPath", the introduction of the package

After. Writes the C3P0 configuration file to the spring configuration file of course we

Call it "Spring.xml" and fill in the following characters:

<!--implement DataSource with C3P0, connect pool--
<bean id= "C3p0datasource"

class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "Driverclass" >
<value>com.mysql.jdbc.Driver</value>
</property>
<property name= "Jdbcurl" >
<value>jdbc:mysql://localhost:3308/myoa?

useunicode=true&characterencoding=utf-

8&zerodatetimebehavior=converttonull</value>
</property>
<property name= "User" >
<value>root</value>
</property>
<property name= "Password" >
<value>root</value>
</property>
<!--the minimum number of connections that are kept in the connection pool. -
<property name= "Minpoolsize" >
<value>5</value>
</property>

<!--The maximum number of connections that are kept in the connection pool. Default:15--
<property name= "Maxpoolsize" >
<value>30</value>
</property>

<!--the number of connections obtained when initializing, the value should be between Minpoolsize and Maxpoolsize

。 Default:3--
<property name= "Initialpoolsize" >
<value>10</value>
</property>

<!--maximum spare time, unused in 60 seconds the connection is discarded. If 0, it will never be discarded.

Default:0--
<property name= "MaxIdleTime" >
<value>60</value>
</property>

<!--c3p0 the number of connections fetched at the same time when the connection in the connection pool is exhausted.



Default:3--
<property name= "Acquireincrement" >
<value>5</value>
</property>

<!--the standard number of references for JDBC. Used to control preparedstatements loaded in the data source

Number. But because the pre-cached statements
belong to a single connection instead of the entire connection pool. So setting this parameter requires consideration

To a variety of factors.
Assuming that both maxstatements and maxstatementsperconnection are 0, the cache is

Shut down. Default:0-->
<property name= "Maxstatements" >
<value>0</value>
</property>

<!--Check the spare connections in all connection pools every 60 seconds. Default:0--
<property name= "Idleconnectiontestperiod" >
<value>60</value>
</property>

<!--defines the number of times to try repeatedly after a new connection has failed to get from the database.

Default:30

-
<property name= "Acquireretryattempts" >
<value>30</value>
</property>

<!--getting a connection failure will cause all the threads waiting on the connection pool to get the connection to throw an XOR

Often.

But the data source still works.
Keep. and continue trying to get the connection the next time you call Getconnection (). Such as

Set to True, then try to
When a connection failure is obtained, the data source declares that it has been fractured and closed permanently.

Default:

False-->
<property name= "Breakafteracquirefailure" >
<value>true</value>
</property>

<!--use it only when you need it because of high performance consumption. If set to true then in each

Submitted by a connection.
Officer the validity of the test. It is recommended to use Idleconnectiontestperiod or

Automatictesttable
and other methods to improve the performance of the connection test.

Default:false--
<property name= "Testconnectiononcheckout" >
<value>false</value>
</property>
</bean>

There is a specific note in it. If only a part of the Java Foundation should be able to read

The The rest is to use DataSource in spring to invoke and use the database connection

The pool, assuming that it is useful, there is no need to set it,

It's all set.

C3P0 Configuration in Spring

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.