struts2+spring3+hibernate3正确的配置proxool

    整整一天都在折腾S2SH+proxool,终于配置成功了,都快崩溃了!哈哈...不过始终相信:皇天不负有心人。但是还是要说一下,今天费了这么大劲这么长时间没有配置成功的一个原因就是网上一些人写得文章极其的不负责任,这句话说的不太准确,但是他们写的肯定也有欠缺的地方,最起码版本号应该注明吧,版本号是框架中最大的一个“陷阱”,虽然我不确定是不是由于版本号导致我配置不成功,但是做为人所用的教程,应该尽力清楚吧!还有就是批判一下转载文章的那些人,不知道你们在转载别人文章之前配置成功没有...疑问....。

    下面我说一下我的配置过程,先说过程再说遇到的问题。具体的版本号是:struts2.2.1、spring3.0.5、hibernate3.6、proxool0.9.1。

    (一)在tomcat的context.xml中配置proxool,这样就不用写proxool.xml或proxool.properties配置文件了。

<?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context reloadable="true"> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" factory="org.logicalcobwebs.proxool.ProxoolDataSource" proxool.alias="DBpool" user="root" password="root" proxool.jndi-name="mysqljndi" proxool.driver-url="jdbc:mysql://127.0.0.1:3306/blog?characterEncoding=utf-8" proxool.driver-class="com.mysql.jdbc.Driver" proxool.house-keeping-sleep-time="40000" proxool.maximum-connection-count="100" proxool.minimum-connection-count="20" proxool.maximum-connection-lifetime="18000000" proxool.simultaneous-build-throttle="20" proxool.recently-started-threshold="40000" proxool.overload-without-refusal-lifetime="50000" proxool.maximum-active-time="60000" proxool.verbose="true" proxool.trace="true" proxool.fatal-sql-exception="Fatal error" proxool.prototype-count="2" proxool.statistics-log-level="ERROR"> </Resource> </Context>  

(二)配置spring的数据源,在applicationContext.xml中配置以下代码

<bean id="myData" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/mysql</value> </property> </bean> <!-- 创建hibernate的sessionfactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="myData" /> </property> .... 

(三) 在web.xml中配置proxool Admin便于管理

<servlet> <servlet-name>Admin</servlet-name> <servlet-class> org.logicalcobwebs.proxool.admin.servlet.AdminServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>Admin</servlet-name> <url-pattern>/admin</url-pattern> </servlet-mapping> 

    配置到这就大功告成了,但是还要注意要把proxool.xml和proxool-cglib.xml文件拷贝到tomcat的lib文件夹中。要不会报找不到类的错误!

 

    下面我说一下今天主要遇到的错误:今天我配置主要用了两种方案:一个是用hibernate配置proxool,另一个是用spring配置proxool。具体的方法网上一抓一把这是一本比较好的,比较详细的教程http://www.iteye.com/topic/913892,其他的配置方法大同小异。但是我没有配置成功,不知道什么原因!

    但是错误的愿意主要就是spring、proxool的加载顺序问题,因为spring先于proxool所以启动tomcat始终报错:不能加载连接池,找不到连接池,No suitable driver found for proxool。有一种方案是修改proxool的源码:并在加载spring之前加载proxool,修改之后的为:

package cn.wt.listener; import java.io.File; import java.util.Enumeration; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.logicalcobwebs.proxool.ProxoolException; import org.logicalcobwebs.proxool.configuration.JAXPConfigurator; import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; /** * @author wangtao */ public class ProxoolListener implements ServletContextListener { private static final Log LOG = LogFactory.getLog(ProxoolListener.class); private static final String XML_FILE_PROPERTY = "xmlFile"; private static final String PROPERTY_FILE_PROPERTY = "propertyFile"; private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown"; @SuppressWarnings("unused") private boolean autoShutdown = true; public void contextDestroyed(ServletContextEvent arg0) { System.out.println("destroy database pool...."); } public void contextInitialized(ServletContextEvent contextEvent) { ServletContext context = contextEvent.getServletContext(); //对应servlet的init方法中ServletConfig.getServletContext() String appDir = contextEvent.getServletContext().getRealPath("/"); Properties properties = new Properties(); Enumeration names = context.getInitParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = context.getInitParameter(name); if (name.equals(XML_FILE_PROPERTY)) { try { File file = new File(value); if (file.isAbsolute()) { JAXPConfigurator.configure(value, false); } else { JAXPConfigurator.configure(appDir + File.separator + value, false); } } catch (ProxoolException e) { LOG.error("Problem configuring " + value, e); } } else if (name.equals(PROPERTY_FILE_PROPERTY)) { try { File file = new File(value); if (file.isAbsolute()) { PropertyConfigurator.configure(value); } else { PropertyConfigurator.configure(appDir + File.separator + value); } } catch (ProxoolException e) { LOG.error("Problem configuring " + value, e); } } else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) { autoShutdown = Boolean.valueOf(value).booleanValue(); } else if (name.startsWith("jdbc")) { //此处以前是PropertyConfigurator.PREFIX改为jdbc properties.setProperty(name, value); } } if (properties.size() > 0) { try { PropertyConfigurator.configure(properties); } catch (ProxoolException e) { LOG.error("Problem configuring using init properties", e); } } } }  

但是我没有按这种方式来配置。好了差不多就这么多吧,希望看到的人对你们有帮助!!

参考:http://www.360doc.com/content/10/0916/11/1542811_54056566.shtml、http://wt8414.iteye.com/blog/252675

 

你可能感兴趣的:(spring,Hibernate,struts,properties,String,File)