让hibernate 的实现方法具有事务性,用spring事务管理器进行切面管理。
若不具备事务性,
1)dataSource池 无法管理 连接池,执行一条DB操作,将会打开一个新连接,直到消耗了20个连接后,挂掉,且无法自动回复db 连接。
2)没有事务性的系统,运行的安全性,可想而知了。
下面是一种比较被合理采用的,也是比较灵活的,注解方式 声明 给 spring 事务管理器。
配置文件比较简单,无须扩展,只要在操作DB时,方法声明 @Transactional 即可。
1: applicationContext.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"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/wysosdb?useUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="softdn"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="10"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="false"></property>
</bean>
<!-- hibernate 的会话工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop><!-- 显示SQL,为了方便测试 -->
</props>
</property>
<property name="mappingResources">
<list><!-- 映射文件 -->
<value>db/table/Member.hbm.xml</value>
</list>
</property>
</bean>
<!-- 定义事务管理器(声明式的事务)(针对hibernate的事务管理器) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 配置HibernateTransactionManager时需要依注入 SessionFactory的引用 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 采用@Transaction注解的方式使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 把Session工厂注入给hibernateTemplate -->
<!-- 解释一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在执行时自动建立 HibernateCallback 对象,例如:load()、get()、save、delete()等方法。 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg>
<ref local="sessionFactory"/>
</constructor-arg>
</bean>
<!-- 以下注入是Member Table 开始于 55555 结束于55555 -->
<bean id="memberDao" class="db.dao.impl.MemberDaoImpl" scope="singleton">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="memberService" class="service.impl.MemberServiceImpl" scope="prototype">
<property name="memberDao" ref="memberDao"></property>
</bean>
</beans>
2:daoImpl 方法调用
/**
* 根据 VO 查询 List
*/
@Transactional
public List<Task> listTaskByExample(Task task, Order order)
throws Exception {
List<Task> list = null;
// -----1,Check session is open or not, and open it
// -----2,Create criteria for the bean class
Criteria cs = this.getSession().createCriteria(Task.class);
cs.add(Example.create(task));
if (null != task.getTaskId()){
cs.add(Expression.eq("taskId", task.getTaskId()));
}
// -----3,Build the Order
if (order != null){
cs.addOrder(order);
}
// -----4,Exe
list = cs.list();
// -----5,finally Close session
// -----6,Return
return list;
}
分享到:
相关推荐
包含了spring事务的两种使用方式,xml和注解,有数据库
spring 事务基于注解模式
本代码使用H2内存数据库演示spring事务使用,包括编程式事务,声明式事务@Transactional使用,自定义事务事务注解实现自定义事务管理器
Spring 与 各框架的组各下来,版本就特别的多,针对简单的 使用注解来管理事务的,研究了两天,要不网上写的不清楚,要不版本都很旧了,所以就是不回滚,Spring 的配置太灵活了,在加上新手,根本就不可能明白Spring...
spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...
spring boot 纯注解方法事务控制回滚,注解+简单配置文件使用多线程demo
spring使用注解进行事物管理例子,里面有说明.
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
spring3hibernate4注解式事务管理
声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,更清爽。spring注解事务实现demo
springMVC+Mybatis3.1+spring3.1.2(包含事务详解,代码诠释,含数据库文件...展示了增、删、改、查、注解、拦截器、spring事务配置(亲测成功),sql文 件!!赶紧来下载给好评!!! web project 完全可以跑起来!!!
本文处理spring+mybatis,或springjdbc,对oracle事务不能提交的问题分析与解决
4、了解Spring事务管理的两种方式; 5、掌握基于XML和Annotation的声明式事务管理的使用。 二.实验内容 (1)使用Spring JDBC实现书店的购书过程,即有如下一个BookShopDao接口,编写BookShopDaoImp类实现该接口中的...
SpringJDBC注解事务.zip,SpringJDBC注解事务.zip
spring mvc hibernate 实现事务管理 jar包管理用maven,启动服务器用jetty,里边还有memcached数据库,但是本例子没有用到。
Wicket6.0_Spring3.1_Hibernate4.1_EJB全注解实例。采用JTA事务管理,在glassfish3.1.2和postgresql9测试通过。参考网上的资料整理。
Spring+Hibernate注解声明式事务实例
事务简介 二、程序举例环境搭建 o1. 创建数据表 ...三、使用 Spring 的事务注解管理事务 o1. 声明事务管理器 o2. 开启注解驱动 o3. 完整Spring配置文件 o4. 业务层 public 方法加入事务属性 o5. 测试
spring3hibernate4注解是事务管理
本篇文章主要介绍了Spring 使用注解方式进行事务管理配置方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧