网上很多朋友讲到了hibernate怎么用proxool
单独的怎么配置proxool,却很少把struts2和spring2还有hibernate整合起来的.花了一天的时候,才把配置整成功,分享出来给大家,希望可以少走弯路吧.
这里和大家讲一下,为什么要用tomcat来启动数据库连接池.除了资源控制一块,还有一个很大的原因是因为struts2要求spring的加载方式为<listener>
而spring会整合hibernate,在实例化的时候,会去加载proxool数据链接池.
因为proxool的加载方式是通过<servlet>方式来的,加载顺序在<listener>之后,也就是spring的后面加载.这样加载会不成功.
所以proxool的加载只能是在spring之前加载.网上有一个网友把proxool的加载方式改成了 <listener> 放在spring之前加载,这样太麻烦了,还要改源码.
于是这个方式成本就最低了.
当然,也有网友提到
写道
直接在application.xml配上
<prop key="hibernate.proxool.xml">proxool.xml</prop>
以上引用,我配置后,并没有作用.估计那位网友使用的版本和我的不同吧.反正我试了半天是没有成功.
tomcat/conf/context.xml 配置proxool.
加在
元素的前面 proxool的配置元素,就不多讲了,网上有很多详细的介绍.这里给大家一个链接:http://wallimn./blog/486550
- <Resource name="jdbc/mysql"
- auth="Container"
- type="javax.sql.DataSource"
- factory="org.logicalcobwebs.proxool.ProxoolDataSource"
- proxool.alias="DBPool"
- user="root"
- password="root"
- delegateProperties="foo=bar"
- proxool.jndi-name="mysqljndi"
- proxool.driver-url="jdbc:mysql://127.0.0.1:3306/webgame?characterEncoding=utf-8"
- proxool.driver-class="com.mysql.jdbc.Driver"
- proxool.house-keeping-sleep-time="40000"
- proxool.house-keeping-test-sql="SELECT ''"
- 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
- />
现在就不需要单独的来配置proxool.properies 和proxool.xml了.
记得把proxool-0.9.1.jar和proxool-cglib.jar包复制到tomcat/lib文件夹下,否则会报找不到
- org.logicalcobwebs.proxool.ProxoolDataSource
错误.把包放到自己项目下的话,会报这个错,但是也可以运行的.
接下来是配置web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java./xml/ns/javaee"
- xmlns:xsi="http://www./2001/XMLSchema-instance"
- xsi:schemaLocation="http://java./xml/ns/javaee
- http://java./xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
- </context-param>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <!-- 由于已经通过tomcat的jndi来加载了数据连接,所以这样里以不需要了
- <servlet>
- <servlet-name>ServletConfigurator</servlet-name>
- <servlet-class>
- org.logicalcobwebs.proxool.configuration.ServletConfigurator
- </servlet-class>
- <init-param>
- <param-name>propertyFile</param-name>
- <param-value>
- WEB-INF\classes\proxool.properties
- </param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- -->
- <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>/proxool.admin</url-pattern>
- </servlet-mapping>
-
-
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- </web-app>
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www./schema/beans"
- xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:p="http://www./schema/p"
- xmlns:aop="http://www./schema/aop" xmlns:tx="http://www./schema/tx"
- xmlns:context="http://www./schema/context"
- xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-2.5.xsd http://www./schema/tx http://www./schema/tx/spring-tx-2.5.xsd http://www./schema/aop http://www./schema/aop/spring-aop-2.5.xsd http://www./schema/context http://www./schema/context/spring-context-2.5.xsd">
- <import resource="classes/xml/spring/games.xml" />
- <import resource="classes/xml/spring/blcx.xml" />
-
-
- <bean id="dataSource"
- class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName">
- <value>java:comp/env/jdbc/mysql</value>
- </property>
- </bean>
-
-
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- <prop key="hibernate.proxool.existing_pool">true</prop>
- <prop key="hibernate.format.sql">true</prop>
- <prop key="hibernate.show.sql">true</prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/webgame/account/form/WebgameAccount.hbm.xml</value>
- <value>com/webgame/account/form/WebgameAccountIndex.hbm.xml</value>
- <value>com/webgame/pay/form/WebgameAccountFinancingLog.hbm.xml</value>
- <value>com/webgame/pay/form/WebgameCategoriesRate.hbm.xml</value>
- </list>
- </property>
-
- </bean>
-
-
-
- <bean id="hibernateTemplate"
- class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
-
-
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="sessionFactory" />
- </property>
- </bean>
-
-
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
-
- <property name="transactionManager">
- <ref local="transactionManager" />
- </property>
- <property name="transactionAttributes">
-
- <props>
- <prop key="save">PROPAGATION_REQUIRED</prop>
- <prop key="dele*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
-
-
- <bean id="accountIndexDao" class="com.webgame.account.dao.impl.AccountIndexDao"
- p:hibernateTemplate-ref="hibernateTemplate"
- />
- </beans>
以上配置基本解决了启动方面的问题.
使用:
- package com.webgame.account.dao.impl;
-
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- import org.apache.log4j.Logger;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.criterion.Restrictions;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
-
- import com.webgame.account.dao.interfaces.IAccountDao;
- import com.webgame.account.form.AccountBean;
-
-
-
-
-
-
- public class AccountDao extends HibernateDaoSupport implements IAccountDao{
- private static final Logger logger = Logger.getLogger(AccountDao.class);
-
-
-
-
-
-
-
- public AccountBean getAccountBean(String sql) throws SQLException {
- AccountBean form = new AccountBean();
- List list = getHibernateTemplate().find(sql);
- if (list.size() == 1)
- form = (AccountBean) list.get(0);
- return form;
- }
-
-
-
-
-
-
- @Override
- public int getAccountCount(String sql) throws SQLException {
- int reInt = 0;
- List list = getHibernateTemplate().find(sql);
- reInt = list.size();
- return reInt;
- }
-
-
-
-
-
-
- @SuppressWarnings("unchecked")
- @Override
- public List<AccountBean> getAccountList(String sql) throws SQLException {
- List<AccountBean> list = new ArrayList<AccountBean>();
- list = getHibernateTemplate().find(sql);
- return list;
- }
-
-
- @Override
- public boolean checkAccountExist(final String property,final String value) {
- return (Boolean)getHibernateTemplate().execute(new HibernateCallback(){
- @SuppressWarnings("unchecked")
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- List<AccountBean> list = session.createCriteria(AccountBean.class).add(Restrictions.eq(property, value)).list();
- if(list != null && list.size() > 0 )
- return true;
- else return false;
- }
- });
- }
-
- @Override
- public AccountBean getAccountBeanByName(final String accountName) {
- return (AccountBean)getHibernateTemplate().execute(new HibernateCallback(){
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- return session.createCriteria(AccountBean.class).add(
- Restrictions.eq("accountName", accountName))
- .setMaxResults(1).uniqueResult();
- }
- });
- }
-
- public boolean saveAccount(AccountBean bean) throws SQLException {
- bean.setInTime(new Date());
- getHibernateTemplate().save(bean);
- return true;
- }
-
- }