spring使用的jar包如下
hibernate使用的jar包如下:
Spring使用的spring3.1.1,hibernate使用的4.1.0版本。
applicationContext.xml配置如下:
<!-- define PlatformTransactionManager (declarative transaction) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- ensure that the above transactional advice runs for any execution
of an operation defined by the FooService interface -->
<aop:config>
<aop:pointcut expression="execution(* com.henu.strawhat.service.impl.*.*(..))" id="myPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
</aop:config>
<!-- config the transactionnal advice -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="save" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
Spring3有两种事务处理配置;一种是基于xml的声明式事务管理。另外一种是基于@Transaction的声明式事务管理。本文主要是针对前者进行配置。
下面是DAO持久层代码:
session.beginTransation();
session.getTransaction().commit();