why does transaction roll back on RuntimeException but not SQLException

This is defined behaviour. From the docs: Any RuntimeException triggers rollback, and any checked Exception does not. This is common behaviour across all Spring transaction APIs. By default, if a RuntimeException is thrown from within the transactional code, the transaction will be rolled back. If a checked exception (i.e. not a RuntimeException) is thrown, then … Read more

Difference between LockModeType Jpa

I would first differentiate between optimistic and pessimistic locks, because they are different in their underlying mechanism. Optimistic locking is fully controlled by JPA and only requires additional version column in DB tables. It is completely independent of underlying DB engine used to store relational data. On the other hand, pessimistic locking uses locking mechanism … Read more

now() default values are all showing same timestamp

That is expected and documented behaviour: From the manual: Since these functions return the start time of the current transaction, their values do not change during the transaction. This is considered a feature: the intent is to allow a single transaction to have a consistent notion of the “current” time, so that multiple modifications within … Read more

How to configure transaction management for working with 2 different db in Spring?

The javadoc for JpaTransactionManager has some advice on this: This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make … Read more

Can a COMMIT statement (in SQL) ever fail? How?

COMMIT may fail. You might have had sufficent resources to log all the changes you wished to make, but lack resources to actually implement the changes. And that’s not considering other reasons it might fail: The change itself might not fit the constraints of the database. Power loss stops things from completing. The level of … Read more

Creating a post commit when using transaction in Spring

You could get exactly what you want by a simpler way, with TransactionSynchronizationManager and TransactionSynchronization With TransactionSynchronizationManager, you have static methods to get information about current transaction, and you can register a TransactionSynchronization wich allows you to automatically do a post-commit as you call that TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization(){ void afterCommit(){ //do what you want to do … Read more