Please see following (sudo) code:
public interface FooDAO extends JpaRepository<Foo, Long> {
// Query method1 - Database needs 10 mins to finish updating
void updateQueryMethod1()
// Query method2
void updateQueryMethod2()
}
@Transactional
public class FooService {
FooDao fooDao;
void fooServiceMethod() {
fooDao.updateQueryMethod1();
fooDao.updateQueryMethod2();
}
}
Assume updateQueryMethod1()
is a time consuming query in database. It takes 10 minutes to finish updating in the database.
My question is, in fooServiceMethod()
method, will updateQueryMethod2()
wait for updateQueryMethod1()
to finish updating in database?
If updateQueryMethod2()
does not wait for updateQueryMethod1()
to finish updating in database, how to make updateQueryMethod2()
wait for updateQueryMethod1()
to finish its updating first?
CodePudding user response:
Methods or instructions are executed sequentialy and updateQueryMethod2()
will be executed after updateQueryMethod1()
. It also depends if your methods are excuted in a synchronous or an asynchronous way. You have many ways to implement what you want:
- Observer Design Pattern:
updateQueryMethod2()
will be executed as soon asupdateQueryMethod1()
notifyupdateQueryMethod2()
that he finished is job - Put a
if
statement that will executeupdateQueryMethod2()
whenupdateQueryMethod1()
execution result returnstrue
CodePudding user response:
Updates must acquire an exclusive lock over the TABLE they're modifying to avoid write-write conflicts,and since the first method(Transaction) acquired that ,the second method must wait until the exclusive lock is released so it can perform its update,so it must wait as long as the lock is retained by the other method(Transaction),that's why btw long running transactions are a detriment to a database performance because of this ,which is refered to as lock retention.Hope this helps.