spring下配置tomcat jdbc pool 报找不到"com.mysql.jdbc.Driver"类
初始配置
1 <!--tomcat jdbc pool数据源配置--> 2 <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 3 <property name="poolProperties"> 4 <bean class="org.apache.tomcat.jdbc.pool.PoolProperties"> 5 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 6 <property name="url" value="${database.url}"/> 7 <property name="username" value="${database.username}"/> 8 <property name="password" value="${database.password}"/> 9 <property name="jmxEnabled" value="true"/> 10 <property name="testWhileIdle" value="true"/> 11 <property name="testOnBorrow" value="true"/> 12 <property name="testOnReturn" value="false"/> 13 <property name="validationInterval" value="30000"/> 14 <property name="validationQuery" value="SELECT 1"/> 15 <property name="timeBetweenEvictionRunsMillis" value="30000"/> 16 <property name="initialSize" value="10"/> 17 <property name="maxActive" value="40"/> 18 <property name="minIdle" value="10"/> 19 <property name="maxIdle" value="20"/> 20 <property name="maxWait" value="10000"/> 21 <property name="minEvictableIdleTimeMillis" value="30000"/> 22 <property name="logAbandoned" value="false"/> 23 <property name="removeAbandoned" value="true"/> 24 <property name="removeAbandonedTimeout" value="60"/> 25 <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/> 26 </bean> 27 </property> 28 </bean> 29 30 31 <!-- 定义实体管理器工厂 --> 32 <bean id="entityManagerFactory" 33 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 34 <property name="persistenceUnitName" value="myJPA" /> 35 <property name="dataSource" ref="dataSource" /> 36 <property name="jpaVendorAdapter"> 37 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 38 <property name="showSql" value="true"/> 39 <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/> 40 <property name="generateDdl" value="true" /> 41 <property name="database" value="MYSQL" /> 42 </bean> 43 </property> 44 <property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/> 45 <property name="jpaProperties"> 46 <props> 47 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> 48 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 49 <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> 50 <prop key="hibernate.connection.url">${database.url}</prop> 51 <prop key="hibernate.connection.username">${database.username}</prop> 52 <prop key="hibernate.connection.password">${database.password}</prop> 53 <prop key="hibernate.max_fetch_depth">3</prop> 54 <prop key="hibernate.jdbc.fetch_size">18</prop> 55 <prop key="hibernate.jdbc.batch_size">10</prop> 56 <prop key="hibernate.hbm2ddl.auto">update</prop> 57 <prop key="hibernate.show_sql">true</prop> 58 <prop key="hibernate.format_sql">true</prop> 59 <prop key="javax.persistence.validation.mode">none</prop> 60 </props> 61 </property> 62 </bean>
启动时报错,
1 ... 2 3 java.sql.SQLException: com.mysql.jdbc.Driver 4 at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254) ~[tomcat-jdbc.jar:na] 5 at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182) ~[tomcat-jdbc.jar:na] 6 at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:702) ~[tomcat-jdbc.jar:na] 7 at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:634) ~[tomcat-jdbc.jar:na] 8 at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:488) ~[tomcat-jdbc.jar:na] 9 at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144) ~[tomcat-jdbc.jar:na] 10 11 ... 12 13 at javax.management.remote.rmi.RMIConnectionImpl.access$300(Unknown Source) [na:1.8.0] 14 at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(Unknown Source) [na:1.8.0] 15 at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(Unknown Source) [na:1.8.0] 16 at javax.management.remote.rmi.RMIConnectionImpl.invoke(Unknown Source) [na:1.8.0] 17 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:201402101544] 18 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:201402101544] 19 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:201402101544] 20 at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0] 21 at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source) [na:1.8.0] 22 at sun.rmi.transport.Transport$1.run(Unknown Source) [na:1.8.0] 23 at sun.rmi.transport.Transport$1.run(Unknown Source) [na:1.8.0] 24 at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0] 25 at sun.rmi.transport.Transport.serviceCall(Unknown Source) [na:1.8.0] 26 at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source) [na:1.8.0] 27 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source) [na:1.8.0] 28 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source) [na:1.8.0] 29 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0] 30 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0] 31 at java.lang.Thread.run(Unknown Source) [na:1.8.0] 32 Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 33 at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.8.0] 34 at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.8.0] 35 at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0] 36 at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0] 37 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0] 38 at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0]
这篇文章解释了原因 http://stackoverflow.com/questions/4790589/sqlexception-w-tomcat-7-0-jdbc-connection-pool-and-mysql , 大概意思是加载 数据库连接驱动jar包(tomcat?) 和 tomcat jdbc包(spring?) 不是同一个Classloader, 所以需要改为如下配置
1 <!-- 使用spring 加载jdbc驱动 --> 2 <bean id="dataSourceTemplate" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 3 <property name="driverClassName" value="${database.driver}" /> 4 <property name="url" value="${database.url}" /> 5 <property name="username" value="${database.username}" /> 6 <property name="password" value="${database.password}" /> 7 </bean> 8 9 <!--tomcat jdbc pool数据源配置--> 10 <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 11 <!-- 通过引用加载数据库信息 --> 12 <property name="dataSource" ref="dataSourceTemplate"/> 13 14 <property name="jmxEnabled" value="true"/> 15 <property name="testWhileIdle" value="true"/> 16 <property name="testOnBorrow" value="true"/> 17 <property name="testOnReturn" value="false"/> 18 <property name="validationInterval" value="30000"/> 19 <property name="validationQuery" value="SELECT 1"/> 20 <property name="timeBetweenEvictionRunsMillis" value="30000"/> 21 <property name="initialSize" value="10"/> 22 <property name="maxActive" value="40"/> 23 <property name="minIdle" value="10"/> 24 <property name="maxIdle" value="20"/> 25 <property name="maxWait" value="10000"/> 26 <property name="minEvictableIdleTimeMillis" value="30000"/> 27 <property name="logAbandoned" value="false"/> 28 <property name="removeAbandoned" value="true"/> 29 <property name="removeAbandonedTimeout" value="60"/> 30 <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/> 31 </bean>
其他配置不变