0
votes

i have a database.xml to define spring transaction like

<aop:config proxy-target-class="true">
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.compass.utils..*.*(..))" />
</aop:config>

and my dao,service are all in the utils package or subpackage like:

com.compass.utils.db.dao
com.compass.utils.service

if my service class is like

    public class AlarmService {
         @autowired
         private AlarmDao alarmDao;
         @autowired
         private PositionDao positionDao;
         @autowired
         private AccountDao accountDao;
      public void testService(){
          alarmDao.save(a);
          positionDao.save(b);
          accountDao.save(c);
     }
   }

how many transactions are created in invoke the testService? 4 transactions?three transactions for save invoke ,and one for testService?or just one transaction?

if 4 transactions are created,how to rollback if save or testService throw exception? maybe positionDao.save(b) throw exception,just only transaction of positionDao.save rollback,and alarmDao save complete or all save rollback because of testService transaction?

if i change AlarmService package path ,make it out of com.compass.utils,like com.compass.service,what effect the transactions about my question?

when AlarmService is in com.compass.service package,and i annotation @Transactional in testService method, what effect the transactions about my question?

@Transactional can @Override the database.xml configure?

thanks for your help and suggestion in advance!

1

1 Answers

1
votes

how many transactions are created in invoke the testService? 4 transactions?three transactions

Only one TX will be created and if any exception occurs in one of the save methods, the TX will be rolled back.

if i change AlarmService package path ,make it out of com.compass.utils,like com.compass.service,what effect the transactions about my question?

3 TXs will be created and each one will be committed or rolled back individually.

when AlarmService is in com.compass.service package,and i annotation @Transactional in testService method, what effect the transactions about my question?

None, since it is not in configured packages, Spring does not advise that, so your annotation will be ignored. This will behave like the second question and will create 3 transactions.

@Transactional can @Override the database.xml configure?

Yes, Spring is capable of using XML and annotation configurations alongside together and always uses the most specific configuration which is the annotation in this case.

P.S. You can change all these with different propagation levels. These are all based on default behavior.