The configuration data source for spring notes

Source: Internet
Author: User
Tags connection pooling

Any DAO Access database, you first need to configure the data source, the data source defines a common configuration to access the database. You have a data source to create a data template, and then you inject the data template into the DAO, and you manipulate the database by invoking the appropriate method in the data Access template.

The commonly used definition data source technologies are:

1.JDBC, the most basic configuration, because there is no pooling management, each connection request will return a new connection, not recommended in the production environment.

<BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource"Destory-method= "Close">    < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"/>    < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/test"/>    < Propertyname= "username"value= "root"/>    < Propertyname= "Password"value= "root"/></Bean>

2. Connection pooling (C3P0,DBCP), which typically uses connection pooling technology in production environments, improves performance and avoids the ability to turn connections on or off every time you access it.

The data source is defined in the DBCP way, and there is a commons-dbcp-1.2.1.jar under Tomcat's lib package .<BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"Destory-method= "Close">    < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"/>    < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/test"/>    < Propertyname= "username"value= "root"/>    < Propertyname= "Password"value= "root"/>    <!--Initialize the number of connections -    < Propertyname= "InitialSize"value= "5"/>     <!--maximum number of connections in the same time pool, 0 unrestricted -     < Propertyname= "Maxactive"value= "Ten"/>  </Bean>The data source is defined here in C3P0 and requires the import of C3P0 jar packages, including C3p0-0.9.5.1.jar,mchange-commons-java-0.2.10.jar, if the database is Oracle, You also need to import C3p0-oracle-thin-extras-0.9.5.1.jar method one:<BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"Destory-method= "Close">    < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"/>    < Propertyname= "Jdbcurl"value= "Jdbc:mysql://localhost:3306/test"/>    < Propertyname= "User"value= "root"/>    < Propertyname= "Password"value= "root"/>    <!--Initialize the number of connections -    < Propertyname= "Initialpoolsize"value= "5"/>     <!--maximum number of connections in the same time pool, 0 unrestricted -     < Propertyname= "Maxpoolsize"value= "Ten"/>  </Bean>method Two: Create a c3p0.properties file under the Classpath, Combopooleddatasource will automatically find the file under the classpath<BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"Destory-method= "Close"/>c3p0.properties file Format (more reference official documentation): C3p0.driverclass=com.mysql.jdbc.driver C3p0.jdbcurl=jdbc:mysql://localhost : 3306/test c3p0.user=root c3p0.password=root c3p0.maxpoolsize=10 c3p0.minpoolsize=2 c3p0.initiaPoolSize=2

You can also import the properties file through Spring's <context:property-placeholder location= "Jdbc.properties"/> and then fill in value with the corresponding key value in method one (for example, $ {jdbc.driverclassname}), the benefit of this approach is that the keys in the properties file can be defined arbitrarily, as long as the values correspond.

The configuration data source for spring notes

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.