Freaky Thommi
Freaky Thommi

Reputation: 746

@Transactional (readOnly = false, propagation = Propagation.REQUIRED) is throwing exception

I am using spring with hibernate and using spring transaction manager. I have the below method which is called from another method which is transactional.

@Transactional (readOnly = true, propagation = Propagation.REQUIRED)
public Map<String, String> getAllProperties ()
{  }

The problem I am facing is ; if I run this I am getting the below exception

Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: commit failed

But instead if I change the annotation to the blow one or remove the annotation properties it just works fine

 @Transactional (readOnly = false, propagation = Propagation.REQUIRED)
public Map<String, String> getAllProperties ()
{  }

I felt its kind of strange as I am changing only the readOnly property.

can somebody please explain this

Upvotes: 2

Views: 10132

Answers (2)

Nagaraj Joshi NRJ
Nagaraj Joshi NRJ

Reputation: 41

If you want to modify some data in database its not possible with readOnly = true. Its ok if you want to retrieve some data set.

So when it comes to UPDATE or INSERT use readOnly = false.

GOOD LUCK.

Upvotes: 4

AdityaKeyal
AdityaKeyal

Reputation: 1228

It seems you are modifying the list/object that was received from hibernate within the getAllProperties() method.

If you provide more details of the implementation it would help.

In the meantime, do not perform any operation on the list/object returned by Hibernate (except a get* operation). Even if you remove an element from the list (for filtration) that would try to change the underlying values in your database.

Upvotes: 0

Related Questions