Start to directly use Spring to obtain the data source configured in the Tomcat container through JNDI. Tomcat should be the DBCP connection pool by default. No problem. Everything is OK. We recommend that you use the C3P0 connection pool for Hibernate and Spring, so I tried to configure it. I didn't expect it to be completed after half an afternoon! The content on the Internet is dazzled and mixed, so it is not as reliable as yourself! Directly go to the Code, and the following is a problem!
Configure two places:
1. context. xml under Tomcat \ conf
2. Spring configuration file
Tip:
1. If you want to optimize the performance of the connection pool, you need to set the parameters. For details, refer to the official website! The most reliable official website! The official website is Wang Dao!
C3P0: http://www.mchange.com/projects/c3p0/
DBCP: http://commons.apache.org/proper/commons-dbcp/
2. C3P0 jar package should be placed in the WEB-INF \ lib, also need to put in Tomcat \ lib package
3. The configuration of the project and different databases can be decoupled through the JNDI configuration.
Context. xml
12345678910111213141516171819202122232425 |
<! -- DBCP connection pool --> < Resource name = "jdbc/gdDB" auth = "Container" type = "javax.sql.DataSource" username = "scott" password = "tiger" driverClassName = "oracle.jdbc.driver.OracleDriver" url = "jdbc:oracle:thin:@localhost:1521:orcl" testOnBorrow = "true" testOnReturn = "true" testWhileIdle = "true" validationQuery = "SELECT COUNT(*) FROM DUAL" maxIdle = "30" maxWait = "5000" maxActive = "100" initialSize = "10" /> <! -- C3P0 connection pool --> < Resource name = "jdbc/DB" auth = "Container" user = "scott" password = "tiger" driverClass = "oracle.jdbc.driver.OracleDriver" jdbcUrl = "jdbc:oracle:thin:@localhost:1521:orcl" maxPoolSize = "30" minPoolSize = "1" initialPoolSize = "5" acquireIncrement = "2" idleConnectionTestPeriod = "60" maxIdleTime = "60" factory = "org.apache.naming.factory.BeanFactory" type = "com.mchange.v2.c3p0.ComboPooledDataSource" /> |
ApplicationContext. xml
12345678910111213 |
<! -- Configure the JNDI data source --> < bean id = "dataSource" class = "org.springframework.jndi.JndiObjectFactoryBean" > < property name = "jndiName" > <!-- <value>java:comp/env/jdbc/c3p0DB</value> --> < value >java:comp/env/jdbc/DB</ value > </ property > </ bean > < bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > < property name = "dataSource" > < ref bean = "dataSource" /> </ property > </ bean > |
Problems:
1.javax.naming.NamingException: No set method found for property: username
Solution:
In context. xml, the configuration parameters of the C3P0 connection pool are incorrect. Copy the preceding DBCP configuration directly. Later, we found that the parameter names of DBCP and C3P0 are different, including case sensitivity!
2.org. springframework. beans. ConversionNotSupportedException: Failed to convert property value of type 'com. mchange. v2.c3p0. mbean. C3P0PooledDataSource'
Required type 'javax. SQL. DataSource 'for property 'datasource'
Solution:
In context. xml, the type value of the C3P0 connection pool is incorrect. If it is set to type = "com. mchange. v2.c3p0. ComboPooledDataSource", it will be OK!
Other questions are hard to remember! You can use it. You have time to study the Optimization Configuration and internal implementation principles!
In addition, I found many posts on the internet saying that there are three parts to be configured: web. xml, But I can detect that the web. xml file does not need to be configured. Am I wrong? If you have any good opinions, please contact us and learn from each other!