Henry
Henry

Reputation: 87

two methods in one transaction

I have a DAO class with two following methods:

@Transactional
public void save() throws Exception {

}

@Transactional
public void save2() {

}

In the service class I call those methods as follows:

public void processDAO() {
    dao.save();
    dao.save2();
}

My question is, will those methods run as part of the same transaction or in independent transactions or none of these?

Thanks and best regards,

Upvotes: 4

Views: 2253

Answers (2)

Ge Jun
Ge Jun

Reputation: 113

they will run in independent transactions.
you can refrence to Showing a Spring transaction in log to check it. if you want processDAO methond in the same transaction you can add @Transactional on processDAO method

Upvotes: 4

dunni
dunni

Reputation: 44515

If the processDAO() method or the class of this method or a method/class which is higher in the call stack is also annotated with @Transactional, then they will run in the same transaction, otherwise in two different transactions.

Upvotes: 4

Related Questions