现在SPRING中比较流行通过注解的方式来完成注入,事务处理等。我也是刚开始用,在事务处理的应用中遇到了问题,请教下。
我的APPLICATIONCONTEXT.XML是这样的
<?xml version="1.0" encoding="UTF-8"?> <!--
- Application context definition for JPetStore's business layer. -
Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's
"contextConfigLocation").
-->
<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: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/tx 
        
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       "
default-autowire="byName" default-lazy-init="true">
<context:annotation-config />
<context:component-scan base-package="com.roger" />
<!--
这一项配置了需要扫描的包,包含子包
--> <!-- database -->
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="jdbcUrl">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="user">
<value>roger</value>
</property>
<property name="password">
<value>dake123456</value>
</property> <!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40"></property> <!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1"></property> <!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1"></property> <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20"></property>
</bean> <!-- define Hibernate SessionFactory  -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/roger/bean/Mould.hbm.xml</value> </list>
</property>
</bean>
<!-- end database config --> <!-- transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- end transaction -->
</beans>DAO的实现类是这样的
@Repository("mouldDAO")
@Transactional
public class MouldDAOImpl  implements MouldDAO {
@Resource
 private SessionFactory sessionFactory;
  public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Transactional(propagation=Propagation.REQUIRED)
public void addMould(Mould mould) {
// TODO Auto-generated method stub
Session session = sessionFactory.openSession();
session.save(mould);

//Transaction tran = session.beginTransaction();
//tran.commit();
}}
我写了个测试方法,在执行的时候也不报错,但是在数据库中就是没有插入记录,如果我手工显示的写COMMIT(),就可以插入成功。
请哪位高手看看,如果同时注解的方式,怎么写才能实现事务处理。