網(wǎng)上很多朋友講到了hibernate怎么用proxool
單獨的怎么配置proxool,卻很少把struts2和spring2還有hibernate整合起來的.花了一天的時候,才把配置整成功,分享出來給大家,希望可以少走彎路吧.
這里和大家講一下,為什么要用tomcat來啟動數(shù)據(jù)庫連接池.除了資源控制一塊,還有一個很大的原因是因為struts2要求spring的加載方式為<listener>
而spring會整合hibernate,在實例化的時候,會去加載proxool數(shù)據(jù)鏈接池.
因為proxool的加載方式是通過<servlet>方式來的,加載順序在<listener>之后,也就是spring的后面加載.這樣加載會不成功.
所以proxool的加載只能是在spring之前加載.網(wǎng)上有一個網(wǎng)友把proxool的加載方式改成了 <listener> 放在spring之前加載,這樣太麻煩了,還要改源碼.
于是這個方式成本就最低了.
當然,也有網(wǎng)友提到
寫道
直接在application.xml配上
<prop key="hibernate.proxool.xml">proxool.xml</prop>
以上引用,我配置后,并沒有作用.估計那位網(wǎng)友使用的版本和我的不同吧.反正我試了半天是沒有成功.
tomcat/conf/context.xml 配置proxool.
加在
元素的前面 proxool的配置元素,就不多講了,網(wǎng)上有很多詳細的介紹.這里給大家一個鏈接: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
- />
現(xiàn)在就不需要單獨的來配置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>
-
- <!-- 由于已經(jīng)通過tomcat的jndi來加載了數(shù)據(jù)連接,所以這樣里以不需要了
- <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;
- }
-
- }