Struts2 Spring2.5 Hibernate3.2 proxool9.1 tomcat6.18配置整合

网上很多朋友讲到了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.

加在

</Context>

 元素的前面 proxool的配置元素,就不多讲了,网上有很多详细的介绍.这里给大家一个链接:http://wallimn.iteye.com/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.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/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>
	
	<!-- 加载spring文件 -->
	<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.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<import resource="classes/xml/spring/games.xml" />
	<import resource="classes/xml/spring/blcx.xml" />

	<!-- 使用tomcat jndi数据连接池 ,这里项目中的servlet就不用配置启动连接池了-->
	<bean id="dataSource"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:comp/env/jdbc/mysql</value>
		</property>
	</bean>
	<!-- Hibernate设置 -->
	<!-- 会话工厂设置,读取Hibernate数据库配置信息 -->
	<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>

	<!-- Spring设置 -->
	<!-- 会话模板 -->
	<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">
		<!-- 事务拦截器bean 需要依赖注入一个事务管理器-->
		<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>

	<!--注入到dao类里-->
	<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);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.webgame.accout.dao.interfaces.IAccountDao#getAccountBean(java.lang.String,
	 *      java.lang.String[])
	 */
	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;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.webgame.account.dao.interfaces.IAccountDao#getAccountCount(java.lang.String)
	 */
	@Override
	public int getAccountCount(String sql) throws SQLException {
		int reInt = 0;
		List list = getHibernateTemplate().find(sql);
		reInt = list.size();
		return reInt;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.webgame.account.dao.interfaces.IAccountDao#getAccountList(java.lang.String)
	 */
	@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;
	}

}

你可能感兴趣的:(spring,tomcat,Hibernate,bean,配置管理)