Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off auto commit and give explicit commit usign @transactional

How can I turn off auto commit and give explicit commit using @transactional?

I have multiple operations and I want them to get committed after all select and update are done. Reason being the select is done based on some field as null and this data is used somewhere and then update is done to some records. So before new records come, I have to change the field as to some value and avoid selection of new data i.e select only those record that got updated

like image 286
user Avatar asked Oct 28 '25 10:10

user


2 Answers

You just need to put the @Transaction annotation around all the work you want to be committed:

@Autowired
private Manager1 manager1;

@Autowired
private Manager2 manager2;

@Transactional
public void doStuff() {
   manager1.do();
   manager2.do();
}

Everything in method doStuff() will all be committed together, unless there is an exception thrown, in which case it will all be rolled back.

like image 121
Essex Boy Avatar answered Oct 30 '25 01:10

Essex Boy


I would recommend you to use Programmatic approach for this case.

Programmatic transaction management: This means that you have manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
Vs
Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.

Perhaps, alternative way for your questions by Programmatic transaction management. Eg.

/** DataSourceTransactionManager */
    @Autowired
    private PlatformTransactionManager txManager;

    public void yourMethod() {
    try {

         // Start a manual transaction.
         TransactionStatus status = getTransactionStatus();

         your code...
         .....
         .....

        //your condition 
        txManager.commit(status);
        //your condition 
        txManager.rollback(status);

         } catch (YourException e) {
        //your condition 
        txManager.rollback(status);

        }       
    }    

/**
     * getTransactionStatus
     *
     * @return TransactionStatus
     */
    private TransactionStatus getTransactionStatus() {
        DefaultTransactionDefinition dtd = new DefaultTransactionDefinition();
        dtd.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        dtd.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        dtd.setReadOnly(false);

        return txManager.getTransaction(dtd);
    }

Note: It does not mean you need to always use one approach like Programmatic transaction management. I prefer mixed approach. Please use easy way like Declarative transaction for simple database services, otherwise, just control with Programmatic transaction in your services will save your logic easily.

like image 37
Ye Win Avatar answered Oct 30 '25 00:10

Ye Win



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!