2

I have a Java Spring application with an Oracle DB and Hibernate. In my controller, I'm calling a DAO to retrieve some data. The DAO method proceeds until it reaches the return statement and then it fails to return to the controller. No exception is thrown. Instead, it times out. It's something like this

Controller:

 @Autowired
    DAO dao; 

     public @ResponseBody int controller(){
          //stuff     
          System.out.println(1); 
          Map<Long, DBObj> objs = dao.getObjMap(ids); 
          System.out.println(3); 
          //other stuff
     }

DAO:

@Transactional
 public Map<Long, DBObj> getObjMap(List<Long> ids){
      //stuff
      System.out.println(2)
      return objs; 
 }

Output:

 1
 2

As far as I can tell, it is retrieving from the DB correctly, so it doesn't seem to be a DB issue. Other database calls work fine.

From the debugger, it seems to be hanging somewhere inside the return statement. Specifically, it seems to be hung on SocketInputStream.java while trying to call socketRead0

EDIT: The problem was to do with sorting. I sorted the child objects of the retrieved object. On return, Hibernate was attempting to make additional database calls and hanging as a result. I resolved this by passing the parent object to the calling method and then sorting in the calling method instead of the DAO.

DFL
  • 173
  • 1
  • 14
  • 2
    Since you are in a transaction you could be running into a deadlock or some other query/insert that's just taking too long at the database level. – JustinKSU May 16 '16 at 15:04
  • I agree with JustinKSU ... does your commit go through? Do you really need a transaction for a read operation? – Pat B May 16 '16 at 15:05
  • Looks like the operations in your getObjMap is taking much time. Could you please share those lines of codes too – PeaceIsPearl May 16 '16 at 15:10
  • Instead of tracing, you should use debugger to see if method actually finished. – Boris Treukhov May 16 '16 at 15:13
  • Right now, I'm testing it on my local environment, so there shouldn't be a deadlock. The database isn't the issue because I can print the retrieved data right before the return statement, and it all looks fine. When I remove the @Transactional, I get "org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" so it does seem to be necessary – DFL May 16 '16 at 15:18

2 Answers2

0

Try this :

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Transactional(
    timeout = 3600,
    rollbackFor = { RuntimeException.class, ApplicationCheckedException.class },
    noRollbackFor = { ApplicationCheckedNoRollbackException.class, InternalNoRollbackException.class })
public @interface LongTx {

    // Empty.

}

Annotate your method with @LongTx

@LongTx
 public Map<Long, DBObj> getObjMap(List<Long> ids){
      //stuff
      System.out.println(2)
      return objs; 
 }
PeaceIsPearl
  • 329
  • 2
  • 6
  • 19
  • Just tried this. Doesn't help- it hangs in the same spot. I don't think it's a database issue because other database calls work fine. – DFL May 16 '16 at 16:17
  • Did you try to debug – PeaceIsPearl May 16 '16 at 16:21
  • Yes, it is hanging somewhere inside the return statement. – DFL May 16 '16 at 16:24
  • What are the operations you are doing..like what us the code //stuff in that method. Can you share ? – PeaceIsPearl May 16 '16 at 16:30
  • Basically, I'm retrieving a list of objects based on a list of ids. Then I put them into a map so I can easily get them by their ids. Before putting them into the map, I do a sort on the children objects of those objects, but that's entirely programmatic and doesn't involve an additional DB call. Should be simple. My hibernate criteria is just "criteria.add(Restrictions.in("ID", ids));" – DFL May 16 '16 at 16:34
  • "No exception is thrown. Instead, it times out" Do you see a ttansaction timeout exception ? – PeaceIsPearl May 16 '16 at 16:38
  • I get "Thread "WebContainer : 6" (0000017f) has been active for 677271 milliseconds and may be hung." – DFL May 16 '16 at 17:03
  • Specifically, it seems to be hung on SocketInputStream.java while trying to call socketRead0 – DFL May 16 '16 at 17:05
  • Please check, if this helps you : http://stackoverflow.com/questions/27042528/thread-webcontainer-0-00000029-has-been-active-for-647279-milliseconds-and – PeaceIsPearl May 16 '16 at 20:20
0

Just see to it there is no lock on the Database from say some other transaction, meaning see to it that if there is some lock mechanism used in the code.

There may be some other transaction holding a lock on the records in the table.

shankarsh15
  • 1,947
  • 1
  • 11
  • 16
  • As far as I can tell, there is no problem with the database. I can call other methods that access the database just fine, even ones that use the same tables. – DFL May 16 '16 at 16:28
  • Please check this post http://stackoverflow.com/questions/23753510/timeout-implementation-of-jpa-transactions-and-session-invalidation – shankarsh15 May 16 '16 at 17:35