2 royjing RoyJing 于 2015.06.25 09:55 提问

hibernate 手动控制事务回滚失败 1C

spring配置:





5
20
100
5
100

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" /> 
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
            hibernate.show_sql=true
            hibernate.format_sql=false
        </value>
    </property>
    <property name="packagesToScan" value="com.art8k.platform.*.*.bean"/>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true"/>
        <tx:method name="find*" read-only="true"/>
        <tx:method name="list*" read-only="true"/>
        <tx:method name="*" rollback-for="Exception"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="operation" expression="execution(* com.art8k.platform..service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="operation" />
</aop:config>
<bean id="dao" class="com.art8k.platform.dao.IDaoManager">
     <property name="sessionFactory">
        <ref bean="sessionFactory"/>    
    </property>
</bean>
<bean id="baseService" abstract="true" class="com.art8k.platform.service.BaseService">
    <property name="dao">
        <ref bean="dao"/>
    </property>
</bean>

BaseService.java
public BaseService() {
    if (SpringUtil.getWebApplicationContext()!=null && SpringUtil.getWebApplicationContext().getBean("dao") != null) {
        this.dao = (IDaoManager) SpringUtil.getWebApplicationContext().getBean("dao");
    }
}

/**
 * 数据库操作 dao
 */
@SuppressWarnings("unchecked")
@Resource
protected IDaoManager dao;

@SuppressWarnings("unchecked")
public void setDao(IDaoManager dao) {
    this.dao = dao;
}

@SuppressWarnings("unchecked")
public IDaoManager getDao() {
    return dao;
}

IDaoManager.java
public class IDaoManager<T, P, PK extends Serializable> extends HibernateDaoSupport implements GeneraDAO<T, P, PK> {

/**
 * <p>
 * 持久化业务逻辑数据对象 ,无返回值
 * </p>
 */
public void saveIObject(final T object) {
    getHibernateTemplate().save(object);
}

/**
 * <p>
 * 持久化业务逻辑数据对象,新增或者更新 ,无返回值
 * </p>
 * 
 * @param object
 *            T 业务逻辑数据对象
 */
public void saveOrUpdateIObject(final T object) {
    getHibernateTemplate().saveOrUpdate(object);
}

我自己的service集成BaseService,我在service里的一个方法内插入两条数据,如果出现异常,把本次对数据库的所有操作回滚,现在问题是回滚失败,代码如下:
public int saveProduct(HmProduct hmProduct) {
int result = ConstanData.SUCCESSCODE;
Session sessions = null;
sessions = this.getDao().getHibernateTemplate().getSessionFactory().openSession();
sessions.beginTransaction();
try {
sessions.save(hmProduct);
HmProductLog hmProductLog = new HmProductLog();
hmProductLog.setProductId(hmProduct.getId());
hmProductLog.setPrice(hmProduct.getPrice());
hmProductLog.setCreateTime(new Date());
sessions.save(hmProductLog);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        sessions.getTransaction().rollback();
        result = ConstanData.FAILURECODE;
        e.printStackTrace();
    }finally{
        sessions.close();
    }
    return result;
}

1个回答

u011619071
u011619071   2015.06.25 10:59

先看spring版本,低版本需要设置编程式事务,在类中使用curentsession,与当前线程绑定。

Csdn user default icon
上传中...
上传图片
插入图片