Four data sources are configured in Spring) and four in Spring.

Source: Internet
Author: User

Configure four data sources in Spring (conversion) and four types in Spring

Original http://blog.csdn.net/orclight/article/details/8616103

 

No matter what persistence technology is used, data sources must be defined. Spring provides four different data source configuration methods:

Spring data source (DriverManagerDataSource), DBCP data source, C3P0 data source, and JNDI data source.

1. spring Data Source

DriverManagerDataSource

XML code:

 

[Html]View plaincopy
  1. <Bean id = "dataSource"
  2. Class = "org. springframework. jdbc. datasource. DriverManagerDataSource">
  3. <Property name = "driverClassName" value = "oracle. jdbc. driver. OracleDriver"/>
  4. <Property name = "url" value = "jdbc: oracle: thin: @ 172.19.34.6: 1521: ORCL"/>
  5. <Property name = "username" value = "orclight"/>
  6. <Property name = "password" value = "123456"/>
  7. </Bean>

 

2. DBCP Data Source

DBCP configuration depends on two jar packagesCommons-dbcp.jar, commons-pool.jar.

XML code:

 

[Html]View plaincopy
  1. <Bean id = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource"
  2. Destroy-method = "close">
  3. <Property name = "driverClassName" value = "oracle. jdbc. driver. OracleDriver"/>
  4. <Property name = "url" value = "jdbc: oracle: thin: @ 172.19.34.6: 1521: ORCL"/>
  5. <Property name = "username" value = "orclight"/>
  6. <Property name = "password" value = "123456"/>
  7. </Bean>

 

The code above explains:

BasicDataSource provides the close () method to close the data source. Therefore, you must set the destroy-method = "close" attribute so that the data source can be normally closed when the Spring container is closed. In addition to the required data source attributes, there are also some common attributes:
DefaultAutoCommit: sets whether to use the automatic submission mechanism for connections returned from the data source. The default value is true;
DefaultReadOnly: sets whether the data source can only perform read-only operations. The default value is false;
MaxActive: Maximum number of connections to the database. If it is set to 0, there is no limit;
MaxIdle: Maximum number of waiting connections. If it is set to 0, there is no limit;
MaxWait: the maximum number of waiting seconds. The unit is milliseconds. If the wait time is exceeded, an error message is returned;
ValidationQuery: the SQL statement used to verify whether the connection is successful. The SQL statement must return at least one row of data. For example, you can simply set it to "select count (*) from user ";
RemoveAbandoned: whether to interrupt itself. The default value is false;
RemoveAbandonedTimeout: After several seconds, the data connection is automatically disconnected. If removeAbandoned is set to true, this value is provided;
LogAbandoned: Indicates whether to record the interrupt event. The default value is false;

3. C3P0 Data Source

C3P0 is an open-source JDBC Data Source implementation project. C3P0 depends on the jar packageC3p0. jar.

XML code:

 

[Html]View plaincopy
  1. <Bean id = "dataSource" class = "com. mchange. v2.c3p0. ComboPooledDataSource"
  2. Destroy-method = "close">
  3. <Property name = "driverClass" value = "oracle. jdbc. driver. OracleDriver"/>
  4. <Property name = "jdbcUrl" value = "jdbc: oracle: thin: @ 172.19.34.6: 1521: ORCL"/>
  5. <Property name = "user" value = "orclight"/>
  6. <Property name = "password" value = "123456"/>
  7. </Bean>

 

Like BasicDataSource, ComboPooledDataSource provides a close () method to close the data source. This ensures that the data source can be successfully released when the Spring container is closed.

C3P0 has richer configuration attributes than DBCP. Through these attributes, you can control the data source effectively:
AcquireIncrement: Number of new connections created at one time in C3P0 when connections in the connection pool are used up;
AcquireRetryAttempts: defines the number of times that a new connection fails to be retrieved from the database. The default value is 30;
AcquireRetryDelay: the interval between two connections, in milliseconds. The default value is 1000;
AutoCommitOnClose: by default, all uncommitted operations are rolled back when the connection is closed. The default value is false;
AutomaticTestTable: C3P0 creates an empty table named Test and uses its own query statement for testing. If this parameter is defined, preferredTestQuery is ignored. You cannot perform any operation on this Test table. It will be used for the C3P0 Test. The default value is null;
BreakAfterAcquireFailure: failed connection retrieval will cause all threads waiting for connection retrieval to throw an exception. However, the data source is still valid, and the next call to getConnection () will continue to try to obtain the connection. If it is set to true, the data source will be declared disconnected and permanently closed after the connection fails to be obtained. The default value is false;
CheckoutTimeout: when the connection pool is used up, the client calls getConnection () and waits for the time to obtain the new connection. If it is set to 0, it throws SQLException. Unit: milliseconds. The default value is 0;
ConnectionTesterClassName: test the connection by implementing the ConnectionTester or QueryConnectionTester class. The class name must be set to a fully qualified name. The default value is com. mchange. v2.C3P0. impl. DefaultConnectionTester;
IdleConnectionTestPeriod: How many seconds to check idle connections in all connection pools. The default value is 0, indicating no check;
InitialPoolSize: the number of connections created during initialization. The value should be between minPoolSize and maxPoolSize. The default value is 3;
MaxIdleTime: Maximum idle time. connections that exceed the idle time will be discarded. 0 or negative. The default value is 0;
MaxPoolSize: the maximum number of connections retained in the connection pool. The default value is 15;
MaxStatements: Standard JDBC parameter used to control the number of PreparedStatement loaded in the data source. However, the pre-Cache Statement belongs to a single Connection rather than the entire Connection pool. Therefore, you need to consider multiple factors when setting this parameter. If both maxStatements and maxStatementsPerConnection are 0, the cache is disabled. The default value is 0;
MaxStatementsPerConnection: Maximum number of cached Statement owned by a single connection in the connection pool. The default value is 0;
NumHelperThreads: C3P0 is asynchronous, and slow JDBC operations are completed by helping the process. These operations can effectively improve the performance. Multiple operations can be executed simultaneously through multiple threads. The default value is 3;
PreferredTestQuery: defines the test statements executed for all connection tests. This parameter can significantly improve the test speed when connection tests are used. The test table must exist at the initial data source. The default value is null;
PropertyCycle: the maximum number of seconds that a user can wait before modifying system configuration parameters. The default value is 300;
TestConnectionOnCheckout: because of high performance consumption, use it only when needed. If it is set to true, the validity of each connection is verified when it is submitted. We recommend that you use idleConnectionTestPeriod or automaticTestTable
To improve the connection test performance. The default value is false;
TestConnectionOnCheckin: if it is set to true, the connection validity will be verified when the connection is obtained. The default value is false.

4. JNDI Data Source

If an application is configured on a high-performance Application Server (such as WebLogic or Websphere or tomcat), we may prefer to use the data source provided by the application server. The data source of the application server is opened to callers using JNDI. Spring specifically providesJndiObjectFactoryBeanClass.

Xml code:

 

[Html]View plaincopy
  1. <Bean id = "dataSource" class = "org. springframework. jndi. JndiObjectFactoryBean">
  2. <Property name = "jndiName" value = "java: comp/env/jdbc/orclight"/>
  3. </Bean>

 

 

[Html]View plaincopy
  1. <Beans xmlns = http://www.springframework.org/schema/beans
  2. Xmlns: xsi = http://www.w3.org/2001/XMLSchema-instance
  3. Xmlns: jee = http://www.springframework.org/schema/jee
  4. Xsi: schemaLocation = "http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  6. Http://www.springframework.org/schema/jee
  7. Http://www.springframework.org/schema/jee/spring-jee-2.0.xsd>
  8. <Jee: jndi-lookup id = "dataSource" jndi-name = "java: comp/env/jdbc/orclight"/>
  9. </Beans>

 

References:

1. Spring configuration data source four ways http://www.2cto.com/kf/201301/184061.html

2. Configure Spring data source http://www.cnblogs.com/200911/archive/2012/08/10/2631760.html

3. Spring multi-data source configuration method http://blog.csdn.net/nickbest85/article/details/5510466

4. How to solve the problem of multi-data source in spring framework http://www.iteye.com/topic/72486

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.