S2SH+proxool JNDI 数据源

来源:百度文库 编辑:神马文学网 时间:2024/10/02 16:06:56
Struts2 Spring2.5 Hibernate3.2 proxool9.1 tomcat6.18配置整合
网上很多朋友讲到了hibernate怎么用proxool
单独的怎么配置proxool,却很少把struts2和spring2还有hibernate整合起来的.花了一天的时候,才把配置整成功,分享出来给大家,希望可以少走弯路吧.
这里和大家讲一下,为什么要用tomcat来启动数据库连接池.除了资源控制一块,还有一个很大的原因是因为struts2要求spring的加载方式为
而spring会整合hibernate,在实例化的时候,会去加载proxool数据链接池.
因为proxool的加载方式是通过方式来的,加载顺序在之后,也就是spring的后面加载.这样加载会不成功.
所以proxool的加载只能是在spring之前加载.网上有一个网友把proxool的加载方式改成了 放在spring之前加载,这样太麻烦了,还要改源码.
于是这个方式成本就最低了.
当然,也有网友提到
写道
直接在application.xml配上
proxool.xml
以上引用,我配置后,并没有作用.估计那位网友使用的版本和我的不同吧.反正我试了半天是没有成功.
tomcat/conf/context.xml 配置proxool.
加在
Xml代码 ',1)">

元素的前面 proxool的配置元素,就不多讲了,网上有很多详细的介绍.这里给大家一个链接:http://wallimn.javaeye.com/blog/486550
Xml代码 ',2)">
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文件夹下,否则会报找不到
Xml代码 ',3)">
org.logicalcobwebs.proxool.ProxoolDataSource
错误.把包放到自己项目下的话,会报这个错,但是也可以运行的.
接下来是配置web.xml
Xml代码 ',4)">

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">

index.jsp


log4jConfigLocation
classpath:log4j.properties


struts2

org.apache.struts2.dispatcher.FilterDispatcher



struts2
/*




Admin

org.logicalcobwebs.proxool.admin.servlet.AdminServlet



Admin
/proxool.admin




org.springframework.web.context.ContextLoaderListener



contextConfigLocation
/WEB-INF/applicationContext.xml


applicationContext.xml
Xml代码 ',5)">

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">



class="org.springframework.jndi.JndiObjectFactoryBean">

java:comp/env/jdbc/mysql




class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">




org.hibernate.dialect.MySQLDialect

true
true
true




com/webgame/account/form/WebgameAccount.hbm.xml
com/webgame/account/form/WebgameAccountIndex.hbm.xml
com/webgame/pay/form/WebgameAccountFinancingLog.hbm.xml
com/webgame/pay/form/WebgameCategoriesRate.hbm.xml





class="org.springframework.orm.hibernate3.HibernateTemplate">





class="org.springframework.orm.hibernate3.HibernateTransactionManager">





class="org.springframework.transaction.interceptor.TransactionInterceptor">







PROPAGATION_REQUIRED
PROPAGATION_REQUIRED




p:hibernateTemplate-ref="hibernateTemplate"
/>

以上配置基本解决了启动方面的问题.
使用:
Java代码 ',6)">
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 getAccountList(String sql) throws SQLException {
List list = new ArrayList();
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 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;
}
}