1

I get this warning (HibernateException: Found shared references to a collection), but SOMETIMES, not always, as in other threads. Another interesting thing is that I'm only performing SELECT on db, no inserts at all, nowhere in my whole project.

My DB structure is dead simple:

======        ===========
user          criteria
------        -----------
id     -----> user_id
name          id
...           keywords
======        ...
              ===========

my method looks like this:

private synchronized ArrayList<AutomailingCriteria> getUserCriteria(Integer uid){

        this.session.beginTransaction();
        Query query = this.session.createQuery("from AutomailingCriteria where user.id = :uid");
        query.setParameter("uid", uid);


        ArrayList<AutomailingCriteria> result = new ArrayList<>(query.list());

        if(!this.session.getTransaction().wasCommitted()){
           this.session.getTransaction().commit();        
        }
        return result;
    }

and is being called synchronously by 4 threads.

ex3v
  • 3,518
  • 4
  • 33
  • 55
  • Does the constructor of AutomailingCriteria, one of the setters or any @PostLoadset set any cross references? I reckon that the problem is that one of the 3 above marks the objects as dirty and when hibernate flushes the session, it throws this error. Extend this search to any other object that gets eagerly loaded in this query. – Augusto Feb 10 '14 at 15:17
  • What do you mean by cross reference? If you mean reference to another `hibernate` managed object, then no, except for `oneToMany` between user and criteria objects. Another case is what I did just a second ago - moved `HibernateUtil.getSessionFactory().getCurrentSession();` from thread class constructor (and private class field) to local variable of above `getUserCriteria()` method. Errors stopped showing, but I'm not sure that this is the case, because I randomize data for select on every run. Could it be the case? Btw - hibernate session type is set to `thread`. – ex3v Feb 10 '14 at 15:22
  • As an example, I mean if in the constructor, setter or @PostLoad in AutomailingCriteria you do something like `user.addCriteria(this)`. You might find some pointers in the related questions on the right --> – Augusto Feb 10 '14 at 15:26
  • No,no such thing. Ran my code 10 times after my change that I mentioned in previous commend and things are running good for now. It looks like it could have something in common with session management, since I only changed that. – ex3v Feb 10 '14 at 15:32
  • i got the same thing, also when i do a select – Cui Pengfei 崔鹏飞 Mar 27 '14 at 17:19
  • @CuiPengFei check this thread: http://stackoverflow.com/questions/1692871/found-shared-references-to-a-collection-org-hibernate-hibernateexception?rq=1 – ex3v Mar 28 '14 at 08:49

0 Answers0