一个事务hibernate内的两个事务

时间:2015-06-03 22:54:55

标签: java spring hibernate jpa transactions

我正在使用Struts 1,Spring 3和Hibernate 3维护应用程序。 我想加入两项服务,但每项服务都有1项交易。我正在考虑的解决方案是使用包含两个服务的1个事务进行1个服务。这有可能吗?

- 位于DAO的交易。

*服务Expediente:

public void saveExpedienteCoactivo(CoaTExpc coaTExpc, Auditoria auditoria, String numeracionAutomaticaExpediente) throws Exception, BusinessException{
    IExpedienteCoactivoDAO dao = (IExpedienteCoactivoDAO)getDao();
    try{
//business rules
dao.saveExpedienteCoactivo(coaTExpc, numeracionAutomaticaExpediente);
catch{...}
}

* DAO Expediente:

public synchronized void saveExpedienteCoactivo(CoaTExpc coaTExpc, String numeracionAutomaticaExpediente) throws Exception {
    Session sessionHibernate = this.getSessionFactory().getCurrentSession();
    Savepoint savePoint = sessionHibernate.connection().setSavepoint();
    try{
sessionHibernate.saveOrUpdate(coaTExpc);
sessionHibernate.connection().commit();
}catch(Exception e) {
        sessionHibernate.connection().rollback(savePoint);
        sessionHibernate.getTransaction().rollback();
        System.out.println(this.getClass().getName()+", Error="+e);
        throw new Exception(e);
    }
}

*服务决议:

public void saveResolucion(CoaTRcoa coaTRcoa, Auditoria auditoria) throws Exception, BusinessException{
    IResolucionDAO dao = (IResolucionDAO)getDao();
    try{    
//business rules
dao.saveResolucion(coaTRcoa);
}catch{...}
}

* DAO Resolucion:

public synchronized void saveResolucion(CoaTRcoa coaTRcoa) throws Exception {
    Session sessionHibernate = this.getSessionFactory().getCurrentSession();
    Savepoint savePoint = sessionHibernate.connection().setSavepoint();
    try{
//more code and sql here
sessionHibernate.saveOrUpdate(coaTRcoa);
//more code sql
sessionHibernate.connection().commit();     
}catch(Exception e) {
        sessionHibernate.connection().rollback(savePoint);
        sessionHibernate.getTransaction().rollback();
        System.out.println(this.getClass().getName()+", Error="+e);
        throw new Exception(e);
    }
}

*的applicationContext.xml:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref local="dataSource"/>
    </property>
    <property name="mappingDirectoryLocations">
        <list>
            <value>classpath:gob/osinerg/spjc/configuracion/domain/mapping</value>
            <value>classpath:gob/osinerg/spjc/notificaciones/domain/mapping</value>
            <value>classpath:gob/osinerg/spjc/judicial/domain/mapping</value>
            <value>classpath:gob/osinerg/spjc/coactiva/domain/mapping</value>
            <value>classpath:gob/osinerg/spjc/common/domain/mapping</value>
            <value>classpath:gob/osinerg/alfresco/domain/mapping</value>    
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>  

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

<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref local="transactionManager"/>
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

现在,这可能吗?

public class ServiceTransactionImp implements ServiceTransaction{
public void doTwoSaveTransaction(){
Session sessionHibernate = this.getSessionFactory().getCurrentSession();
Savepoint savePoint = sessionHibernate.connection().setSavepoint();
try{    
serviceExpediente.saveExpedienteCoactivo(coaTExpc,auditoria,numeracionAutomaticaExpediente);
serviceResolucion.saveResolucion(coaTRcoa,auditoria);
sessionHibernate.connection().commit();     
}catch(Exception e) {
        sessionHibernate.connection().rollback(savePoint);
        sessionHibernate.getTransaction().rollback();
        System.out.println(this.getClass().getName()+", Error="+e);
        throw new Exception(e);
    }
}

我不确定它是否有效,因为会话在程序之间是不同的,如果saveResolucion的可能回滚将影响saveExpedienteCoactivo的服务,因为有commit();这个想法是对所有过程执行回滚。

或其他任何解决方案?

0 个答案:

没有答案