There are three methods to obtain the DataSource object in the Spring framework:
1. Obtain DataSource from JNDI.
2. Obtain DataSource from a third-party connection pool.
3. Use DriverManagerDataSource to obtain the DataSource.
1. Obtain DataSource from JNDI
SpringJNDI data source configuration information:
Java: comp/env/jcptperformancejndi
Jcptperformancejndi is the JNDI configured by tomcat or other application servers.
2. Configure JNDI (tomcat ):
Modify the conf/context. xml file or server. xml file in the tomcat directory:
Type = "javax. SQL. DataSource"
MaxActive = "100"
MaxIdle = "30"
MaxWait = "10"
Username = "tysp"
Password = "12345678"
DriverClassName = "oracle. jdbc. driver. OracleDriver"
Url = "jdbc: oracle: thin: @ 192.168.1.35: 1521: orcl"
/>
Add in server. xml Element: Defines the JNDI Resource.
Attribute: Description
Name: Specifies the JNDI name of the Resource.
Auth: Specifies the Manager for Resource management. It has two optional values: Container and Application.
Type: Specifies the Java class name to which the Resource belongs.
Add to element Element: Element is used to specify various parameter values (which can also be configured as above)
Attribute: Description
Factory specifies the factory class name of the generated DataResource
MaxActive specifies the maximum number of active connections in the database connection pool. 0 indicates no restriction.
MaxIdle specifies the maximum number of idle connections in the database connection pool. 0 indicates no restriction.
MaxWait specifies the maximum time for idle connections in the connection pool. An exception is thrown if the value of-1 is exceeded.
Username specifies the username used to connect to the database
Password indicates the password used to connect to the database.
DriverClassName specifies the JDBC driver used to connect to the database
Url specifies the URL to connect to the database
3. Obtain DataSource through JNDI:
Context context = new InitialContext ();
DataSource ds = (DataSource) context. lookup ("java: comp/env/jcptDataSourceJNDI ");
4. Add it to web. xml (this is not required in spring configuration)
DB Connection
Jdbc/TestDB
Javax. SQL. DataSource
Container
Add in web. xml Element: The element indicates that the JNDI resource is referenced in the Web application.
Attribute: Description
Description of the referenced Resource
Res-ref-name specifies the JNDI name of the referenced resource, and The name attribute in the element corresponds
Res-type specifies the Class Name of the referenced resource, and The type attribute in the element corresponds
Res-auth specifies the Manager of the referenced resource, and The auth attribute in the element corresponds
For more information, see http://blog.csdn.net/cyxlzzs/article/details/7352837
2. Obtain DataSource from a third-party connection pool
Spring contains Implementation Packages for two data sources in the third-party dependency package. One is Apache DBCP and the other is C3P0.
1) DBCP class package is located in/lib/jakarta-commons/commons-dbcp.jar, DBCP is a database connection pool dependent on Jakarta commons-pool Object pool mechanism,
Therefore, the/lib/jakarta-commons/commons-pool.jar must be included in the class path.
Org. hibernate. dialect. Oracle9Dialect
True
True
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 closed normally 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;
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;
2) C3P0 is an open source JDBC Data Source implementation project, C3P0 class package is located in Spring lib/c3p0/c3p0-0.9.1.2.jar.
Class = "org. springframework. beans. factory. config. PropertyPlaceholderConfigurer">
Classpath: jdbc. properties
3. Use DriverManagerDataSource to obtain the DataSource
Oracle. jdbc. driver. OracleDriver
Jdbc: oracle: thin: @ 192.168.1.35: orcl
Or_meal
Or_meal
When DriverManagerDataSource establishes a connection, a new connection is created as long as there is a connection.