(绝对有效)sqlserver2000链接proxool连接池,终于配置出spring整合struts,hibernate annotation(pool数据池,sqlserver2000数据库)

首先proxool.xml(需要proxool-0.9.1.jar,proxool-cglib.jar和三个sqlserver包)

<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
  <proxool>
    <alias>pool</alias>
    <driver-url>jdbc:microsoft:sqlserver://localhost:1433;databaseName=tour</driver-url>
    <driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
    <driver-properties>
      <property name="user" value="sa"/>
      <property name="password" value="sa"/>
    </driver-properties>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>20</maximum-new-connections>
<prototype-count>5</prototype-count>
<maximum-connection-count>100</maximum-connection-count>
 <check-valid-connection-sql>select * from t_user</check-valid-connection-sql
>
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>


由于proxool必须先启动,所以需要添加1个类,把proxool在web.xml定义为listener

package cn.wt.listener;   
  
import java.io.File;   
import java.util.Enumeration;   
import java.util.Properties;   
  
import javax.servlet.ServletContext;   
import javax.servlet.ServletContextEvent;   
import javax.servlet.ServletContextListener;   
  
import org.apache.commons.logging.Log;   
import org.apache.commons.logging.LogFactory;   
import org.logicalcobwebs.proxool.ProxoolException;   
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;   
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;   
  
/**
* @author wangtao
*/  
  
public class ProxoolListener implements ServletContextListener   
{   
    private static final Log LOG = LogFactory.getLog(ProxoolListener.class);   
       
    private static final String XML_FILE_PROPERTY = "xmlFile";   
  
    private static final String PROPERTY_FILE_PROPERTY = "propertyFile";   
  
    private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";   
       
    @SuppressWarnings("unused")   
    private boolean autoShutdown = true;   
       
    public void contextDestroyed(ServletContextEvent arg0)   
     {   
         System.out.println("destroy database pool....");   
     }   
  
    public void contextInitialized(ServletContextEvent contextEvent)   
     {   
         ServletContext context = contextEvent.getServletContext(); //对应servlet的init方法中ServletConfig.getServletContext()   
         String appDir = contextEvent.getServletContext().getRealPath("/");   
         Properties properties = new Properties();   
  
         Enumeration names = context.getInitParameterNames();   
        while (names.hasMoreElements()) {   
             String name = (String) names.nextElement();   
             String value = context.getInitParameter(name);   
  
            if (name.equals(XML_FILE_PROPERTY)) {   
                try {   
                     File file = new File(value);   
                    if (file.isAbsolute()) {   
                         JAXPConfigurator.configure(value, false);   
                     } else {   
                         JAXPConfigurator.configure(appDir + File.separator + value, false);   
                     }   
                 } catch (ProxoolException e) {   
                     LOG.error("Problem configuring " + value, e);   
                 }   
             } else if (name.equals(PROPERTY_FILE_PROPERTY)) {   
                try {   
                     File file = new File(value);   
                    if (file.isAbsolute()) {   
                         PropertyConfigurator.configure(value);   
                     } else {   
                         PropertyConfigurator.configure(appDir + File.separator + value);   
                     }   
                 } catch (ProxoolException e) {   
                     LOG.error("Problem configuring " + value, e);   
                 }   
             } else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {   
                 autoShutdown = Boolean.valueOf(value).booleanValue();   
             } else if (name.startsWith("jdbc")) { //此处以前是PropertyConfigurator.PREFIX改为jdbc,因为此源码是0.9.1版本的,与0.9RC3版本有点不一样   
                 properties.setProperty(name, value);   
             }   
         }   
  
        if (properties.size() > 0) {   
            try {   
                 PropertyConfigurator.configure(properties);   
             } catch (ProxoolException e) {   
                 LOG.error("Problem configuring using init properties", e);   
             }   
         }   
     }   
  
}  

然后web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

<display-name>Struts Blank</display-name>
  
  <context-param>
   <param-name>xmlFile</param-name>
   <param-value>WEB-INF/classes/proxool.xml</param-value>
</context-param>
<listener>
   <listener-class>cn.wt.listener.ProxoolListener</listener-class>
</listener>

<context-param>
	<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/classes/beans.xml</param-value>
	</context-param>
   <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

最后是beans.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
           
           
  <context:annotation-config/>
	  <!-- 自动扫描管理Bean,注解方式进行创建 -->
	  <context:component-scan base-package="com"/>
 
  
 
 <bean id="dataSource"     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">     
    <value>org.logicalcobwebs.proxool.ProxoolDriver</value>     
        </property>     
        <property name="url">     
            <value>proxool.pool</value>     
        </property>     
    </bean>     
    <bean id="sessionFactory"     
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">     
        <property name="dataSource">     
            <ref bean="dataSource" />     
        </property>     
        <property name="hibernateProperties">     
            <props>     
                <prop key="hibernate.dialect">     
                   org.hibernate.dialect.SQLServerDialect   
                </prop>     
                <prop key="hibernate.connection.autocommit">true</prop>     
                <prop key="hibernate.show_sql">true</prop>     
                <prop key="hibernate.connection.release_mode">     
                     after_statement      
                </prop>     
            </props>     
        </property>     

    </bean>     
</beans>   

由于第1次配置,所以有错请指出...

还有1种,在网络上找到的说法,本人没试过

  1. proxool:关于这个连接池,网上说的例子也有很多,说是性能最好的一个连接池,坑爹的是,网上讲的它跟spring的整合,有很多都是不对的,直接通过spring配置就可以了,没有必要像网上说的,把listener,改成servlet,直接通过spring的bean配置就可以了,这个在网也有很多。关于这个连接池,我在测试时,发现默认情况下,是不会自动重连的,需要通过如下配置:                                <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
          <property name="url" value="xxxx.xml" />
    </bean> 在xxxx.xml里配置                                                                     <proxool>
        <alias>pool</alias>
        <driver-url>jdbc:mysql://192.168.1.4:3306/MIGRATE_TEST?characterEncoding=utf- 8&autoReconnect=true</driver-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <driver-properties>
            <property name="user" value="website" />
            <property name="password" value="website" />
            <property name="autoReconnect" value="true" />
        </driver-properties>
        <minimum-connection-count>1</minimum-connection-count>
        <maximum-connection-count>8</maximum-connection-count>
        <prototype-count>1</prototype-count>
        <test-before-use>true</test-before-use>
        <house-keeping-sleep-time>60000</house-keeping-sleep-time>
    </proxool> mysql的URL的后面要加上autoReconnect=true,值得注意的是,当使用ibatis时,日志会报warn:registered a statement as closed which wasn't known to be open. 那是因为在ibatis
      package org.springframework.orm.ibatis;
           public class SqlMapClientTemplate extends JdbcAccessor implements SqlMapClientOperations {
                 public <T> T execute(SqlMapClientCallback<T> action) throws DataAccessException {
         ..........
         finally {
            // Only close SqlMapSession if we know we've actually opened it
            // at the present level.
            if (ibatisCon == null) {
                session.close();// 由这段引起的,池ibatisCon不为空时,session永远不会关闭
            }
        }
         }

           }
      将红色部分改为 if (ibatisCon != null),就行了

好了,废话不多说了,需要更详细的资料,可以下载源代码,可以到http://download.csdn.net/detail/qq435967718/4738630下载

数据库到http://download.csdn.net/detail/qq435967718/4757430下载(我也不想分开放,不过上次忘记发了,所以补充上去的,1分,也不贵)

.网站经过tomcat发布后,可以访问这个地址(http://localhost:8080/项目名/index1)或(http://localhost:8080/Tour_Struts9//index1),忘记了,自己2个都试试吧,然后导航点击“用户管理”,再点击“网络管理员”,他就把数据库里的数据拿出来了。


你可能感兴趣的:(spring,spring,Hibernate,Hibernate,struts,struts)