6

Just a little background , I'm a new developer who has recently taken over a major project after the senior developer left the company before I could develop a full understanding of how he structured this. I'll try to explain my issue the best I can.

This application creates several MessageListner threads to read objects from JMS queues. Once the object is received the data is manipulated based on some business logic and then mapped to a persistence object to be saved to an oracle database using a hibernate EntityManager.

Up until a few weeks ago there hasn't been any major issues with this configuration in the last year or so since I joined the project. But for one of the queues (the issue is isolated to this particular queue), the spring managed bean that processes the received object hangs at the method below. My debugging has led me to conclude that it has completed everything within the method but hangs upon completion. After weeks of trying to resolve this I'm at end of my rope with this issue. Any help with this would be greatly appreciated.

Since each MessageListner gets its own processor, this hanging method only affects the incoming data on one queue.

@Transactional(propagation = Propagation.REQUIRES_NEW , timeout = 180)
 public void update(UserRelatedData userData, User user,Company company,...)
 { 
   ...
   ....
   //business logic performed on user object
   ....
   ......
   entityMgr.persist(user);

   //business logic performed on userData object
   ...
   ....
   entityMgr.persist(userData);

   ...
   ....

   entityMgr.flush();

}

I inserted debug statements just to walk through the method and it completes everything including entityMgr.flush.().

Rhouujin
  • 101
  • 2
  • 6

4 Answers4

9

REQUIRES_NEW may hang in test context because the transaction manager used in unit testing doesn't support nested transactions... From the Javadoc of JpaTransactionManager:

* <p>This transaction manager supports nested transactions via JDBC 3.0 Savepoints.
 * The {@link #setNestedTransactionAllowed "nestedTransactionAllowed"} flag defaults
 * to {@code false} though, since nested transactions will just apply to the JDBC
 * Connection, not to the JPA EntityManager and its cached entity objects and related
 * context. You can manually set the flag to {@code true} if you want to use nested
 * transactions for JDBC access code which participates in JPA transactions (provided
 * that your JDBC driver supports Savepoints). <i>Note that JPA itself does not support
 * nested transactions! Hence, do not expect JPA access code to semantically
 * participate in a nested transaction.</i>

So clearly if you don't call (@Java config) or set the equivalent flag in your XML config:

txManager.setNestedTransactionAllowed(true);

or if your driver doesn't support Savepoints, it's "normal" to get problem with REQUIRES_NEW... (Some may prefer an exception "nested transactions not supported")

p3consulting
  • 2,721
  • 2
  • 12
  • 10
4

This kind of problems can show up when underlying database has locks from uncommitted changes.

What I would suspect is some other code made inserts/deletes on userData table(s) outside transaction or in a transaction which takes very long time to execute since it's a batch job or similar. You should analyze all the code referring to these tables and look for missing @Transactional.

mrembisz
  • 12,722
  • 7
  • 36
  • 32
  • This is an old one , but I'm going back to comment just in case someone has a similar issue. It turned out to be an issue with the database. After several meetings with the DBA we finally got it resolved. – Rhouujin Aug 22 '13 at 15:58
  • what was the problem ? – Robocide Jun 09 '15 at 15:52
1

Unfortunately I have the same problem with Propagation.REQUIRES_NEW. Removing it resolves the problem. The debugger shows me that the commit method is hanging (invoked from @Transactional aspect implementation).

The problem appears only in the test spring context, when the application is deployed to the application server it works fine.

rolve
  • 10,083
  • 4
  • 55
  • 75
Vladimir Balandin
  • 3,854
  • 3
  • 17
  • 15
1

Beside this answer, you may also check for the isolation level of your transaction — perhaps it's too restrictive.

Does the update() method hang forever, or does it throw an exception when the timeout elapses?

Community
  • 1
  • 1
MaDa
  • 10,511
  • 9
  • 46
  • 84
  • The update method hangs forever, currently working with a senior developer to see if we can determine if this is a blocking issue on the tables. – Rhouujin Nov 18 '11 at 01:42