Please correct me if I am wrong somewhere.
I am having an issue where my transaction are not being saved to the data base and some sort of racing is occurring which screws up the data. The app is hit in parallel by multiple instances. I have used @Transactional, which I know is to do a transaction with database and the transaction is committed when the method returns.
The question is, does hitting it through multiple instance still maintain this one transaction per hit thing, or it does not handle the situation and data will screw up because of racing?
Can a solution be suggested for the given condition?
1 Answer. Show activity on this post. If you call method2() from method1() within the same class, the @Transactional annotation of the second method will not have any effect because it is not called through proxy, but directly.
If you call a method with a @Transactional annotation from a method with @Transactional belonging to the same Spring Bean , then the called methods transactional behavior will not have any impact on the transaction.
Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.
Annotation Type Transactional. Describes a transaction attribute on an individual method or on a class. When this annotation is declared at the class level, it applies as a default to all methods of the declaring class and its subclasses.
The @Transactional
is not related to synchronization. It just makes sure that your flow either succeeds or fails. Each hit has its own flow and its own success or failure.
I guess what you're experiencing is due to the use of shared data.
For example. If you have a class Foo
that looks like this:
public class Foo {
private static boolean flag = true;
@Transactional
public void doSomething() {
flag = false;
}
}
In this case it doesn't matter that you have many Foo
instances because they all use the same flag
.
Another scenario would be if you have one instance of Foo
(very common if you use something like Spring
) and you have data that is changed for this instance. You can look at the same Foo
example and just remove the static
from flag
:
public class Foo {
private boolean flag = true;
@Transactional
public void doSomething() {
flag = false;
}
}
In either of those cases you need to synchronize the data changes somehow. It has nothing to do with @Transactional
.
That transactions are database transactions and behavior is database engine dependant but it usually works this way:
@Transactional
is not about synchronization.SELECT * FROM MYTABLE FOR UPDATE;
.UPDATE MYTABLE SET A = A + 1;
And it blocks.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With