⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 struts_datasource.txt

📁 包括各种struts的 DataSource的配置方式
💻 TXT
字号:
spring与hibernate3结合,DataSource的配置方式(一) 
在我开发过程中主要用过以下几种配置方式,(连接池用c3p0)

1.dataSource 作为spring的一个bean配置

datasource.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd">

<bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="/WEB-INF/jdbc.properties" />
 </bean>

<bean id="oracleDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass">
   <value>${Oraclejdbc.driverClassName}</value>
  </property>
  <property name="jdbcUrl">
   <value>${Oraclejdbc.url}</value>
  </property>
  <property name="user">
   <value>${Oraclejdbc.username}</value>
  </property>
  <property name="password">
   <value>${Oraclejdbc.password}</value>
  </property>
  <property name="minPoolSize">
   <value>1</value>
  </property>

 <--  达到最大连接数后可以增加的连接数  个 -->
  <property name="acquireIncrement">
   <value>2</value>
  </property>
  <property name="maxPoolSize">
   <value>3</value>
  </property>
  <--  最大闲置时间  秒 -->
  <property name="maxIdleTime">
   <value>600</value>
  </property>
  
  <property name="maxStatements">
   <value>100</value>
  </property>

 <--  闲置的连接测试周期 秒  -->
  <property name="idleConnectionTestPeriod">
   <value>1200</value>
  </property>
   </bean>

<bean id="oracleSessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="mappingResources">
   <list>
      <value>
     com/ce/myceaas/hibernate/mis/pojo/Area.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Dept.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Users.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Employeeinfo.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/AreaDept.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Roles.hbm.xml
    </value>


      <value>
     com/ce/myceaas/hibernate/myce/pojo/Application.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Role.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/User.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Userrole.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Roleprim.hbm.xml
    </value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
   
    <prop key="hibernate.generate_statistics">
     ${hibernate.generate_statistics}
    </prop>
    <prop key="hibernate.dialect">
     ${Oraclehibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">
     ${hibernate.show_sql}
    </prop>
    
    </props>
  </property>
  
   <property name="dataSource">
   <ref local="oracleDataSource" />
   </property>
  
 </bean>

 <bean id="oracleTransactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="oracleSessionFactory" />
 </bean>

</beans>

jdbc.properties 文件:

Oraclejdbc.driverClassName=oracle.jdbc.driver.OracleDriver
Oraclejdbc.url=jdbc:oracle:thin:@172.20.57.52:1521:orcl
Oraclejdbc.username=user
Oraclejdbc.password=password

hibernate.generate_statistics=true

# Property that determines the Hibernate dialect
# (only applied with "applicationContext-hibernate.xml")
#hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect
Oraclehibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.show_sql=true


1.dataSource 作为hibernate的数据源属性配置

datasource.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 <!--  ***********gaojunsheng edit at 2005.12.22**************** -->
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="/WEB-INF/jdbc.properties" />
 </bean>

<bean id="oracleSessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="mappingResources">
   <list>
         <value>
     com/ce/myceaas/hibernate/myce/pojo/Application.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Role.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/User.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Userrole.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Roleprim.hbm.xml
    </value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    
    <prop key="hibernate.connection.provider_class">
     org.hibernate.connection.C3P0ConnectionProvider
    </prop>

    <prop key="hibernate.connection.driver_class">
     ${Oraclejdbc.driverClassName}
    </prop>
    <prop key="hibernate.connection.url">
     ${Oraclejdbc.url}
    </prop>
    <prop key="hibernate.connection.username">
     ${Oraclejdbc.username}
    </prop>
    <prop key="hibernate.connection.password">
     ${Oraclejdbc.password}
    </prop>

    <prop key="hibernate.c3p0.min_size">
     ${hibernate.c3p0.min_size}
    </prop>
    <prop key="hibernate.c3p0.max_size">
     ${hibernate.c3p0.max_size}
    </prop>
    <prop key="hibernate.c3p0.timeout">
     ${hibernate.c3p0.timeout}
    </prop>
    <prop key="hibernate.c3p0.max_statements">
     ${hibernate.c3p0.max_statements}
    </prop>
    <prop key="hibernate.c3p0.acquire_increment">
     ${hibernate.c3p0.acquire_increment}
    </prop>
    <prop key="hibernate.c3p0.idle_test_period">
     ${hibernate.c3p0.idle_test_period}
    </prop>
     
    <prop key="hibernate.generate_statistics">
     ${hibernate.generate_statistics}
    </prop>
    <prop key="hibernate.dialect">
     ${Oraclehibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">
     ${hibernate.show_sql}
    </prop>
    
      <prop key="hibernate.connection.autocommit">true</prop>

   </props>
  </property>
  
  
 </bean>

 <bean id="oracleTransactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="oracleSessionFactory" />
 </bean>

</beans>

jdbc.properties 文件:

Oraclejdbc.driverClassName=oracle.jdbc.driver.OracleDriver
Oraclejdbc.url=jdbc:oracle:thin:@172.20.57.52:1521:orcl
Oraclejdbc.username=user
Oraclejdbc.password=password

hibernate.generate_statistics=true

# Property that determines the Hibernate dialect
# (only applied with "applicationContext-hibernate.xml")
#hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect
Oraclehibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.show_sql=true


hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=3
hibernate.c3p0.timeout=1200
hibernate.c3p0.max_statements=30
hibernate.c3p0.acquire_increment=2
hibernate.c3p0.idle_test_period=60


这两种配置方式的区别是:如果所有的事务都在spring配置的匹配方法中没有问题,如果在spring配置的service外直接调用DAO进行insert或update那么第二种配置方式中如果没有将hibernate.connection.autocommit设置为true,那么虽然可以看到sql语句,但是没有提交,所作的插入或更新无效。





在我开发过程中主要用过以下几种配置方式,(连接池用c3p0)

1.dataSource 作为spring的一个bean配置(jndi方式)

服务器利用tomcat5.5

首先配置tomcat数据源连接池 数据库:mysql

tomcat的数据源连接池有2种方式:1、只作为某个应用的数据源。2、作为全局数据源

第一种数据源配置方式(为本应用配置)

在Context.xml文件中配置如下

 <!--Context debug="0" docBase="D:\project\hibernate\ssosys\ssosys" path="/ssosys" reloadable="true" /-->
<Context debug="0" docBase="D:\project\myce\myceaasmysql\WebRoot" path="/myceaasmysql" reloadable="true">
<Resource auth="Container" description="DB Connection" 
 name="jdbc/myceaas"
 driverClass="com.mysql.jdbc.Driver" 
 jdbcUrl="jdbc:mysql://127.0.0.1:3306/myceaas?useUnicode=true&amp;characterEncoding=GBK"
 user="root" password="admin" 
 maxPoolSize="10" 
 minPoolSize="1" 
 acquireIncrement="1" 
 idleConnectionTestPeriod="60"
 preferredTestQuery="select 1"
 factory="org.apache.naming.factory.BeanFactory" 
 type="com.mchange.v2.c3p0.ComboPooledDataSource" 
  /> 
</Context>

 应用中的web.xml 

<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/myceaas</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

第二种配置方式(作为全局数据源)

tomcat的server.xml

在 <GlobalNamingResources>下配置如下:

<Resource auth="Container"
  name="jdbc/myceaas"
  driverClass="com.mysql.jdbc.Driver" 
  jdbcUrl="jdbc:mysql://127.0.0.1:3306/myceaas?useUnicode=true&amp;characterEncoding=GBK"
  user="root" password="admin" 
  maxPoolSize="10" 
  minPoolSize="1" 
  acquireIncrement="1" 
  idleConnectionTestPeriod="60"
  preferredTestQuery="select 1"
  factory="org.apache.naming.factory.BeanFactory" 
  type="com.mchange.v2.c3p0.ComboPooledDataSource" 
  /> 

在Context.xml文件中配置如下

<ResourceLink
   name="jdbc/myceaas" 
   type="javax.sql.DataSource" 
   global="jdbc/myceaas"/>

 应用中的web.xml 

<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/myceaas</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

下面是spring的配置:

datasource.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd">

<bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="/WEB-INF/jdbc.properties" />
 </bean>

<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/myceaas"/>
    </bean>

<bean id="mysqlSessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="mappingResources">
   <list>
      <value>
     com/ce/myceaas/hibernate/mis/pojo/Area.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Dept.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Users.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Employeeinfo.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/AreaDept.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/mis/pojo/Roles.hbm.xml
    </value>

    </list>
  </property>
  <property name="hibernateProperties">
   <props>
   
    <prop key="hibernate.generate_statistics">
     true    </prop>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect

    </prop>
    <prop key="hibernate.show_sql">
     true

    </prop>
    
    </props>
  </property>
  
   <property name="dataSource">
   <ref local="jndiDataSource" />
   </property>
  
 </bean>

 <bean id="mysqlTransactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="mysqlSessionFactory" />
 </bean>

</beans>

1.dataSource 作为hibernate的数据源属性配置

datasource.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 <!--  ***********gaojunsheng edit at 2005.12.22**************** -->
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="/WEB-INF/jdbc.properties" />
 </bean>

<bean id="mysqlSessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="mappingResources">
   <list>
         <value>
     com/ce/myceaas/hibernate/myce/pojo/Application.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Role.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/User.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Userrole.hbm.xml
    </value>
    <value>
     com/ce/myceaas/hibernate/myce/pojo/Roleprim.hbm.xml
    </value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    
    <prop key="hibernate.connection.provider_class">
   org.hibernate.connection.DatasourceConnectionProvider
    </prop>

    <prop key="hibernate.connection.datasource">

   java:comp/env/jdbc/myceaas
      </prop>
       
    <prop key="hibernate.generate_statistics">
    true    </prop>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect    </prop>
    <prop key="hibernate.show_sql">
    true

    </prop>
    
      <prop key="hibernate.connection.autocommit">true</prop>

   </props>
  </property>
  
  
 </bean>

 <bean id="mysqlTransactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="mysqlSessionFactory" />
 </bean>

</beans>

 

这两种配置方式的区别是:如果所有的事务都在spring配置的匹配方法中没有问题,如果在spring配置的service外直接调用DAO进行insert或update那么第二种配置方式中如果没有将hibernate.connection.autocommit设置为true,那么虽然可以看到sql语句,但是没有提交,所作的插入或更新无效。


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -