乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      S2SH+proxool JNDI 數(shù)據(jù)源

       CevenCheng 2010-09-16

      網(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.

      加在

      Xml代碼 
      1. </Context>  

       元素的前面 proxool的配置元素,就不多講了,網(wǎng)上有很多詳細的介紹.這里給大家一個鏈接:http://wallimn./blog/486550

      Xml代碼 
      1. <Resource name="jdbc/mysql"    
      2.     auth="Container"        
      3.     type="javax.sql.DataSource"    
      4.     factory="org.logicalcobwebs.proxool.ProxoolDataSource"        
      5.     proxool.alias="DBPool"        
      6.     user="root"      
      7.     password="root"        
      8.     delegateProperties="foo=bar"    
      9.     proxool.jndi-name="mysqljndi"       
      10.     proxool.driver-url="jdbc:mysql://127.0.0.1:3306/webgame?characterEncoding=utf-8"        
      11.     proxool.driver-class="com.mysql.jdbc.Driver"    
      12.     proxool.house-keeping-sleep-time="40000"  
      13.     proxool.house-keeping-test-sql="SELECT ''"  
      14.     proxool.maximum-connection-count="100"  
      15.     proxool.minimum-connection-count="20"  
      16.     proxool.maximum-connection-lifetime="18000000"  
      17.     proxool.simultaneous-build-throttle="20"  
      18.     proxool.recently-started-threshold="40000"  
      19.     proxool.overload-without-refusal-lifetime="50000"  
      20.     proxool.maximum-active-time="60000"  
      21.     proxool.verbose="true"  
      22.     proxool.trace="true"  
      23.     proxool.fatal-sql-exception="Fatal error"  
      24.     proxool.prototype-count="2"  
      25.     proxool.statistics-log-level="ERROR  
      26.     />  

       現(xiàn)在就不需要單獨的來配置proxool.properies 和proxool.xml了.

      記得把proxool-0.9.1.jar和proxool-cglib.jar包復(fù)制到tomcat/lib文件夾下,否則會報找不到

      Xml代碼 
      1. org.logicalcobwebs.proxool.ProxoolDataSource  

      錯誤.把包放到自己項目下的話,會報這個錯,但是也可以運行的.

       

      接下來是配置web.xml

      Xml代碼 
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <web-app version="2.5" xmlns="http://java./xml/ns/javaee"  
      3.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
      4.     xsi:schemaLocation="http://java./xml/ns/javaee   
      5.     http://java./xml/ns/javaee/web-app_2_5.xsd">  
      6.     <welcome-file-list>  
      7.         <welcome-file>index.jsp</welcome-file>  
      8.     </welcome-file-list>  
      9.     <context-param>  
      10.         <param-name>log4jConfigLocation</param-name>  
      11.         <param-value>classpath:log4j.properties</param-value>  
      12.     </context-param>  
      13.     <filter>  
      14.         <filter-name>struts2</filter-name>  
      15.         <filter-class>  
      16.             org.apache.struts2.dispatcher.FilterDispatcher  
      17.         </filter-class>  
      18.     </filter>  
      19.     <filter-mapping>  
      20.         <filter-name>struts2</filter-name>  
      21.         <url-pattern>/*</url-pattern>  
      22.     </filter-mapping>  
      23.     <!-- 加載數(shù)據(jù)連接池及監(jiān)控數(shù)據(jù)連接池功能 -->  
      24.     <!-- 由于已經(jīng)通過tomcat的jndi來加載了數(shù)據(jù)連接,所以這樣里以不需要了  
      25.         <servlet>  
      26.         <servlet-name>ServletConfigurator</servlet-name>  
      27.         <servlet-class>  
      28.         org.logicalcobwebs.proxool.configuration.ServletConfigurator  
      29.         </servlet-class>  
      30.         <init-param>  
      31.         <param-name>propertyFile</param-name>  
      32.         <param-value>  
      33.         WEB-INF\classes\proxool.properties  
      34.         </param-value>  
      35.         </init-param>  
      36.         <load-on-startup>1</load-on-startup>  
      37.         </servlet>  
      38.     -->  
      39.     <servlet>  
      40.         <servlet-name>Admin</servlet-name>  
      41.         <servlet-class>  
      42.             org.logicalcobwebs.proxool.admin.servlet.AdminServlet  
      43.         </servlet-class>  
      44.     </servlet>  
      45.     <servlet-mapping>  
      46.         <servlet-name>Admin</servlet-name>  
      47.         <url-pattern>/proxool.admin</url-pattern>  
      48.     </servlet-mapping>  
      49.       
      50.     <!-- 加載spring文件 -->  
      51.     <listener>  
      52.         <listener-class>  
      53.             org.springframework.web.context.ContextLoaderListener  
      54.         </listener-class>  
      55.     </listener>  
      56.     <context-param>  
      57.         <param-name>contextConfigLocation</param-name>  
      58.         <param-value>/WEB-INF/applicationContext.xml</param-value>  
      59.     </context-param>  
      60. </web-app>  

       

      applicationContext.xml

      Xml代碼 
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans xmlns="http://www./schema/beans"  
      3.     xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:p="http://www./schema/p"  
      4.     xmlns:aop="http://www./schema/aop" xmlns:tx="http://www./schema/tx"  
      5.     xmlns:context="http://www./schema/context"  
      6.     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">  
      7.     <import resource="classes/xml/spring/games.xml" />  
      8.     <import resource="classes/xml/spring/blcx.xml" />  
      9.   
      10.     <!-- 使用tomcat jndi數(shù)據(jù)連接池 ,這里項目中的servlet就不用配置啟動連接池了-->  
      11.     <bean id="dataSource"  
      12.         class="org.springframework.jndi.JndiObjectFactoryBean">  
      13.         <property name="jndiName">  
      14.             <value>java:comp/env/jdbc/mysql</value>  
      15.         </property>  
      16.     </bean>  
      17.     <!-- Hibernate設(shè)置 -->  
      18.     <!-- 會話工廠設(shè)置,讀取Hibernate數(shù)據(jù)庫配置信息 -->  
      19.     <bean id="sessionFactory"  
      20.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
      21.         <property name="dataSource" ref="dataSource" />  
      22.         <property name="hibernateProperties">  
      23.             <props>  
      24.                 <prop key="hibernate.dialect">  
      25.                     org.hibernate.dialect.MySQLDialect  
      26.                 </prop>  
      27.                 <prop key="hibernate.proxool.existing_pool">true</prop>  
      28.                 <prop key="hibernate.format.sql">true</prop>  
      29.                 <prop key="hibernate.show.sql">true</prop>  
      30.             </props>  
      31.         </property>  
      32.         <property name="mappingResources">  
      33.             <list>  
      34.                 <value>com/webgame/account/form/WebgameAccount.hbm.xml</value>  
      35.                 <value>com/webgame/account/form/WebgameAccountIndex.hbm.xml</value>  
      36.                 <value>com/webgame/pay/form/WebgameAccountFinancingLog.hbm.xml</value>  
      37.                 <value>com/webgame/pay/form/WebgameCategoriesRate.hbm.xml</value>  
      38.             </list>  
      39.         </property>  
      40.   
      41.     </bean>  
      42.   
      43.     <!-- Spring設(shè)置 -->  
      44.     <!-- 會話模板 -->  
      45.     <bean id="hibernateTemplate"  
      46.         class="org.springframework.orm.hibernate3.HibernateTemplate">  
      47.         <property name="sessionFactory">  
      48.             <ref bean="sessionFactory" />  
      49.         </property>  
      50.     </bean>  
      51.   
      52.     <!-- 事務(wù)管理器 -->  
      53.     <bean id="transactionManager"  
      54.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
      55.         <property name="sessionFactory">  
      56.             <ref local="sessionFactory" />  
      57.         </property>  
      58.     </bean>  
      59.   
      60.     <!-- 配置事務(wù)攔截器-->  
      61.     <bean id="transactionInterceptor"  
      62.         class="org.springframework.transaction.interceptor.TransactionInterceptor">  
      63.         <!-- 事務(wù)攔截器bean 需要依賴注入一個事務(wù)管理器-->  
      64.         <property name="transactionManager">  
      65.             <ref local="transactionManager" />  
      66.         </property>  
      67.         <property name="transactionAttributes">  
      68.             <!-- 下面定義事務(wù)傳播屬性-->  
      69.             <props>  
      70.                 <prop key="save">PROPAGATION_REQUIRED</prop>  
      71.                 <prop key="dele*">PROPAGATION_REQUIRED</prop>  
      72.             </props>  
      73.         </property>  
      74.     </bean>  
      75.   
      76.     <!--注入到dao類里-->  
      77.     <bean id="accountIndexDao" class="com.webgame.account.dao.impl.AccountIndexDao"  
      78.         p:hibernateTemplate-ref="hibernateTemplate"   
      79.     />  
      80. </beans>  

       

      以上配置基本解決了啟動方面的問題.

       

      使用:

      Java代碼 
      1. package com.webgame.account.dao.impl;  
      2.   
      3. import java.sql.SQLException;  
      4. import java.util.ArrayList;  
      5. import java.util.Date;  
      6. import java.util.List;  
      7.   
      8. import org.apache.log4j.Logger;  
      9. import org.hibernate.HibernateException;  
      10. import org.hibernate.Session;  
      11. import org.hibernate.criterion.Restrictions;  
      12. import org.springframework.orm.hibernate3.HibernateCallback;  
      13. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
      14.   
      15. import com.webgame.account.dao.interfaces.IAccountDao;  
      16. import com.webgame.account.form.AccountBean;  
      17.   
      18. /** 
      19.  * 類實現(xiàn)全局所有種類游戲的賬號信息數(shù)據(jù)操作類 
      20.  *  
      21.  *  
      22.  */  
      23. public class AccountDao extends HibernateDaoSupport implements IAccountDao{  
      24.     private static final Logger logger = Logger.getLogger(AccountDao.class);  
      25.   
      26.     /* 
      27.      * (non-Javadoc) 
      28.      *  
      29.      * @see com.webgame.accout.dao.interfaces.IAccountDao#getAccountBean(java.lang.String, 
      30.      *      java.lang.String[]) 
      31.      */  
      32.     public AccountBean getAccountBean(String sql) throws SQLException {  
      33.         AccountBean form = new AccountBean();  
      34.         List list = getHibernateTemplate().find(sql);  
      35.         if (list.size() == 1)  
      36.             form = (AccountBean) list.get(0);  
      37.         return form;  
      38.     }  
      39.   
      40.     /* 
      41.      * (non-Javadoc) 
      42.      *  
      43.      * @see com.webgame.account.dao.interfaces.IAccountDao#getAccountCount(java.lang.String) 
      44.      */  
      45.     @Override  
      46.     public int getAccountCount(String sql) throws SQLException {  
      47.         int reInt = 0;  
      48.         List list = getHibernateTemplate().find(sql);  
      49.         reInt = list.size();  
      50.         return reInt;  
      51.     }  
      52.   
      53.     /* 
      54.      * (non-Javadoc) 
      55.      *  
      56.      * @see com.webgame.account.dao.interfaces.IAccountDao#getAccountList(java.lang.String) 
      57.      */  
      58.     @SuppressWarnings("unchecked")  
      59.     @Override  
      60.     public List<AccountBean> getAccountList(String sql) throws SQLException {  
      61.         List<AccountBean> list = new ArrayList<AccountBean>();  
      62.         list = getHibernateTemplate().find(sql);  
      63.         return list;  
      64.     }  
      65.   
      66.   
      67.     @Override  
      68.     public boolean checkAccountExist(final String property,final String value) {  
      69.         return (Boolean)getHibernateTemplate().execute(new HibernateCallback(){  
      70.             @SuppressWarnings("unchecked")  
      71.             public Object doInHibernate(Session session)  
      72.                     throws HibernateException, SQLException {  
      73.                 List<AccountBean> list = session.createCriteria(AccountBean.class).add(Restrictions.eq(property, value)).list();  
      74.                 if(list != null && list.size() > 0 )  
      75.                     return true;  
      76.                 else return false;  
      77.             }  
      78.         });  
      79.     }  
      80.   
      81.     @Override  
      82.     public AccountBean getAccountBeanByName(final String accountName) {  
      83.         return (AccountBean)getHibernateTemplate().execute(new HibernateCallback(){  
      84.             public Object doInHibernate(Session session)  
      85.                     throws HibernateException, SQLException {  
      86.                 return session.createCriteria(AccountBean.class).add(  
      87.                                 Restrictions.eq("accountName", accountName))  
      88.                                 .setMaxResults(1).uniqueResult();  
      89.             }  
      90.         });  
      91.     }  
      92.           
      93.     public boolean saveAccount(AccountBean bean) throws SQLException {  
      94.         bean.setInTime(new Date());  
      95.         getHibernateTemplate().save(bean);  
      96.         return true;  
      97.     }  
      98.   
      99. }  

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多