Spring3.0.5和Hibernate3.6.0集成的事务控制配置方法
2014-03-14 14:15
543 查看
以下代码示例主要是说明Spring和Hibernate集成后的事务配置方法及使用Spring的AOP实现方法拦截的配置方法()。
环境要求,Spring3.0.5、Hibernate3.6.0
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 定义Proxool数据源 -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=NewDB;"></property>
<property name="user" value="sa"></property>
<property name="password" value="123456"></property>
<property name="maximumConnectionCount" value="3"></property>
<property name="houseKeepingTestSql" value="select getdate()"></property>
</bean>
<!-- 定义Hibernate sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/zywang/model/Student.hbm.xml</value>
<value>com/zywang/model/Teacher.hbm.xml</value>
</list>
</property>
</bean>
<!-- ===================================事务定义开始================================ -->
<!-- Hibernate事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义通知拦截方法 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 事务拦截切面定义 -->
<aop:config>
<aop:pointcut expression="execution(* com.zywang.services.*.*(..))" id="servicesOperation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesOperation"/>
</aop:config>
<!-- ===================================事务定义结束================================ -->
</beans>
环境要求,Spring3.0.5、Hibernate3.6.0
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 定义Proxool数据源 -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=NewDB;"></property>
<property name="user" value="sa"></property>
<property name="password" value="123456"></property>
<property name="maximumConnectionCount" value="3"></property>
<property name="houseKeepingTestSql" value="select getdate()"></property>
</bean>
<!-- 定义Hibernate sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/zywang/model/Student.hbm.xml</value>
<value>com/zywang/model/Teacher.hbm.xml</value>
</list>
</property>
</bean>
<!-- ===================================事务定义开始================================ -->
<!-- Hibernate事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义通知拦截方法 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 事务拦截切面定义 -->
<aop:config>
<aop:pointcut expression="execution(* com.zywang.services.*.*(..))" id="servicesOperation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesOperation"/>
</aop:config>
<!-- ===================================事务定义结束================================ -->
</beans>
相关文章推荐
- Spring3.0和Hibernate3.6集成的事务控制配置方法
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- spring与hibernate集成的session事务控制问题解析
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- Spring和Hibernate集成配置事务管理
- hibernate3.0+ejb3 annotaion配置实战+spring1.21 annotation事务控制
- maven 项目(三)引申--spring集成hibernate+JPA事务配置(扫描注解)
- Hibernate4.3.6 Final+Spring3.0.5配置出错提示及解决方法
- spring-hibernate事务控制的4中方法
- SSH配置 Spring集成Hibernate后,Hibernate配置方法
- 4、spring集成hibernate 及事务控制
- spring和hibernate集成事物管理配置方法
- spring和hibernate集成事物管理配置方法
- spring-hibernate事务控制的4中方法
- spring 与 hibernate 集成事务配置
- spring与hibernate集成:采用tx标签配置声明式事务
- Spring 中基础配置和事务管理的配置和spring托管hibernate配置文件的方法
- Hibernate4.3.6 Final+Spring3.0.5配置出错提示及解决方法
- 关于spring4和hibernate4整合,配置事务报“Cannot unwrap to requested type [javax.sql.DataSource]”错误的解决方法及心得
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)