ssh使用tomcat中配置好的c3p0資料來源

tfvglin發表於2015-07-23

在tomcat的conf\Catalina\localhost\路徑下建立專案對應的xml檔案

具體內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Village" docBase="D:\Workspaces\MyEclipse 10\webproject\Village\WebRoot"  reloadable= "true" privileged="true">

<Resource name="jdbc/village" auth="Container"	
		 type="com.mchange.v2.c3p0.ComboPooledDataSource"  
		factory="org.apache.naming.factory.BeanFactory"  
		driverClass="com.mysql.jdbc.Driver"  
                jdbcUrl="jdbc:mysql://localhost:3306/village"  
                user="root"  
                password="0510"  
                minPoolSize="10"  
                maxPoolSize="200"  
                maxIdleTime="1000" 
		idleConnectionTestPeriod="10"
		initialPoolSize="5" 
		/>
	
</Context>

Spring配置檔案中加入引入資料來源的配置:

 	 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  		<property name="jndiName"><value>java:comp/env/jdbc/village</value></property>
 	 </bean> 

在程式碼配置上只需做如上配置即可。

需要注意的是使用c3p0資料來源需要引入相應的jar包:c3p0-0.9.2.1.jar 和 mchange-commons-java-0.2.3.4.jar。


在實際專案中,我按照如上配置好後,程式開始可以正常執行,但是過一段時間後便會不停的報如下錯誤:


 Illegal access: this web application instance has been stopped already.  Could not load com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:980)
    at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1406)
    at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1378)
    at com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1462)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$1900(BasicResourcePool.java:32)
    at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:1937)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
Exception in thread "Timer-1" java.lang.NoClassDefFoundError: com/mchange/v2/resourcepool/BasicResourcePool$1DestroyResourceTask
    at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:980)
    at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1406)
    at com.mchange.v2.resourcepool.BasicResourcePool.removeResource(BasicResourcePool.java:1378)
    at com.mchange.v2.resourcepool.BasicResourcePool.cullExpired(BasicResourcePool.java:1462)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$1900(BasicResourcePool.java:32)
 
    at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:1937)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    ... 8 more

原因是專案使用了我之前打包好的SSH集合jar包,其中包含了一個com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 包,jar包產生了衝突,刪掉之後程式目前為止都還正常。


PS:後來發現還是會經常報類似的錯誤。某一天,我靈光一閃突然意識到這好像是因為我在開發過程中為了方便開發,將專案的自動重新裝載設定為了true,如下:

<Context path="/Village" docBase="D:\Workspaces\MyEclipse 10\webproject\Village\WebRoot" reloadable= "true" privileged="true">
經過測試發現確實如此,一個正常開啟的專案並不會報這個錯誤。





<pre name="code" class="html">
</pre>

<Context path="/Village" docBase="D:\Workspaces\MyEclipse 10\webproject\Village\WebRoot" reloadable= "true" privileged="true">
<Context path="/Village" docBase="D:\Workspaces\MyEclipse 10\webproject\Village\WebRoot" reloadable= "true" privileged="true">

相關文章