Bad transaction design in Spring

Why does Spring have a bad design of database transactions? Spring suggests using @Transactional annotation to annotate methods which should be run inside the database transaction. This is a bad design. Why?

The database transaction is very important. It is about data consistency. Everything must be absolutely clear when we are working with the database transaction. However, there is a magic in Spring’s @Transactional annotation. And magic is dangerous in software development.

For example? Do you know if you can use @Transactional annotation on private methods? The answer is not clear. It depends on the correct POM (Gradle) configuration, also it depends on the correct Transaction Manager configuration and lots of other things. And if it is not properly configured, the database transactions silently does not work.

This is very bad. After several months of running time on production, you can realize, you have lost consistency because the transactions did not work properly. And you were not aware of it all the time.

The other problem – database transaction should be as small as possible because we lock resources. And it should not be related to the method call. Probably you want to lock small pieces of code, not the whole method. The same rule is applied when we are using the synchronize keyword in Java.

Find the better way how to handle the database transaction.

One thought on “Bad transaction design in Spring”

Leave a Reply

Your email address will not be published. Required fields are marked *