Spirng 4 、Hibernate 4 事务管理

简介:

建议将 Hibernate SessionFactory 交给 Spring 进行事务管理,在 applicationContext.xml 里面配置

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> <!-- hibernate的相关属性配置 --> <property name="hibernateProperties"> <props>
 ...
</bean> <!-- 定义事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> <!-- 自动扫描实体对象 com.waylau.entity 的包结构中存放实体类 --> <property name="packagesToScan" value="com.waylau.entity" /> </bean> 

使用 SessionFactory

@Autowired private SessionFactory sessionFactory;

public Session getSession() {
 return sessionFactory.getCurrentSession();
}

延伸

声明事务

方法1

在调用的类或者方法上加 @Transactional注解

/**
 * @author waylau.com 2015年1月3日
 */ @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@Transactional public class TestSdUserDaoImpl {

 @Autowired private SdUserDaoImpl sdUserDaoImpl;

 @Test public void countAllTest() {
 System.out.println(sdUserDaoImpl.countAll());
 }
 @Test public void findByIdTest() {
 SdUser e = sdUserDaoImpl.findById(1);

 System.out.println(e.getRealname());
 }

}

方法2

用 Spring AOP

<!-- 扫描有注解的文件 base-package 包路径 --> <context:component-scan base-package="com" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 事务执行方式 REQUIRED:指定当前方法必需在事务环境中运行, 如果当前有事务环境就加入当前正在执行的事务环境, 如果当前没有事务,就新建一个事务。 
 这是默认值。 --> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="import*" propagation="REQUIRED" /> <!-- 指定当前方法以非事务方式执行操作,如果当前存在事务,就把当前事务挂起,等我以非事务的状态运行完,再继续原来的事务。 查询定义即可 
 read-only="true" 表示只读 --> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 定义切面,在 com.emsc.service.impl.*.*(..) 中执行有关的hibernate session的事务操作 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.emsc.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> 

这样就定义了那些类或者那些方法需要执行事务

问题

假如如此配置还有事务上的问题,如下:

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
 ...

如果用 aop 有出现问题,请将<aop:config> 改为 <aop:config proxy-target-class="true">


目录
相关文章
|
4月前
|
XML Java 数据库
Spring 事务管理 @Transactional
Spring 事务管理 @Transactional
36 0
|
4月前
|
Java 数据库 Spring
Spring之事务管理
Spring之事务管理
|
10月前
|
Java Spring
spring 事务管理
Spring框架提供了对事务管理的支持,可以通过注解的方式来开启、提交、回滚事务。使用Spring事务管理的步骤如下: 1.添加事务管理器:在Spring配置文件中添加事务管理器,例如使用SpringTransactionManager。 2.添加事务属性:在Spring配置文件中添加事务属性,例如事务隔离级别、事务传播行为、事务超时时间等。 3.添加注解:在需要添加事务管理的方法上添加@Transactional注解。 4.提交事务:在方法执行完成后,使用@Transactional注解的end()方法提交事务。 5.回滚事务:在出现异常时,使用@Transactional注解的r
55 0
|
11月前
|
XML Java 数据库
事务管理-spring
事务管理-spring
46 0
|
12月前
|
XML Java 数据格式
Spring JDBC-使用注解配置声明式事务
Spring JDBC-使用注解配置声明式事务
74 0
|
12月前
|
XML Java 数据格式
Spring JDBC-使用XML配置声明式事务
Spring JDBC-使用XML配置声明式事务
96 0
|
Java 数据库 Spring
Spring 理解 事务管理
Spring 理解 事务管理
73 0
|
存储 安全 Java
Spring @Transactional事务管理
Spring @Transactional事务管理
|
Java Spring
Spring 使用注解方式进行事务管理
Spring 使用注解方式进行事务管理
128 0
|
Oracle 关系型数据库 Java
MyBatis(五) 事务管理
基于Mybaties实现事务操作(附源码)
MyBatis(五) 事务管理