|
From: Juozas B. <ba...@ce...> - 2003-09-27 18:46:21
|
Yes, it is problem in SessionHolder implementation, but it is trivial to
fix:
>
> 01:public class SessionHolder
> 02:{
> 03: private static final boolean DEBUG = true;
> 04: private static ThreadLocal session;
> 05:
> 06: public static Session getSession()
> 07: {
> 08: SessionHolder.connect();
> 09:
> 10: return((Session )SessionHolder.session.get());
> 11: }
> 12:
> 13: public static void connect()
> 14: {
> 15: if (SessionHolder.session.get() == null)
> 16: {
> 17: try
> 18: {
> 19: SessionFactory sessionFactory = new Configuration()
> 20: .configure().buildSessionFactory();
> 21: SessionHolder.session.set( sessionFactory.openSession() );
> 22: } catch (HibernateException e)
> 23: {
> 24: throw new RuntimeException(e);
}
> 26: }
}
> 34:
> 35: public static void disconnect()
> 36: {
> 37: if (SessionHolder.session.get() != null)
> 38: {
> 39: try
> 40: {
> 41: SessionHolder.session.close();
> 42: } catch (HibernateException e) {
throw new RuntimeException(e);
}
> 44: }
> 45: }
> 46:}
>
> QHat do you think? Is there a better way that I should be doing this
> without rewriting a heck of a lot of code? I like the idea of having the
> session establishment self-contained within the SessionHolder object?
>
> Does this SessionHolder seem to be the culprit behind the
> ConcurrentModificationException issues?
>
> > > We seem to be having some problems like this in more than one section
> > > of code. I really don't understand it at all; we suspect some kind of
> > > JVM bug. Unfortunately, no-one has been able to deliver a testcase
> > > that will help me reproduce this on my machine.
>
> > > I can't really do much, until I can reproduce it.
>
> > > P.S. You might also get this if you share a session between two
> > > threads - but at least some occurrences of this problem do not seem to
> > > be caused by this....
>
> > > >I'm receiving an Exception in a Hibernate application I've built, and
> > > >I'm a bit confused as to why this might be happening. The code at
> > > >where the Exception is thrown is quite simple, and I think this might
> > > >be a bug in Hibernate itself. I've searched Google for this to no
> > > >avail, and I'm using the latest recommended Hibernate release as
> > > >available on the site.
>
> > > >The exception I receive has a stacktrace as follows:
>
> > > > java.util.ConcurrentModificationException
> > > > at java.util.AbstractList$Itr.checkForComodification(Unknown
Source)
> > > > at java.util.AbstractList$Itr.remove(Unknown Source)
> > > > at
net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2101)
> > > > at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2061)
> > > > at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2005)
> > > > at net.sf.hibernate.transaction.JDBCTransaction.commit(
> > > > JDBCTransaction.java:57
> > > > )
> > > > at
> > org.express.ftpsrv.ConnectionHandler.<init>(ConnectionHandler.java:72)
> > > > at
> > org.express.ftpsrv.ConnectionListener.accept(ConnectionListener.java:56)
> > > > at
> > org.express.ftpsrv.ConnectionListener.main(ConnectionListener.java:19)
> > > >
> > > >And the code at ConnectionHandler.java:72 that causes this looks
like:
> > > >
> > > > 52:this.ftpSession = new FtpSession();
> > > > 53:
> > > > 54:/* ftpSession.setSessionId() does not operate on the unique
> > > > 55: * 'id' field that is also part of the ftpSession object.
> > > > 56: */
> > > > 57:ftpSession.setSessionId(UniqueGenerator.makeUniqueNumber());
> > > > 58:
> > > > 59:Date currentDate = new Date();
> > > > 60:currentDate.setTime(System.currentTimeMillis());
> > > > 61:this.ftpSession.setBegTime(currentDate);
> > > > 62:
> > > > 63:net.sf.hibernate.Session hSession = null;
> > > > 64:Transaction transaction = null;
> > > > 65:
> > > > 66:try
> > > > 67:{
> > > > 68: hSession = SessionHolder.getSession();
> > > > 69:
> > > > 70: transaction = hSession.beginTransaction();
> > > > 71: hSession.save(this.ftpSession);
> > > > 72: transaction.commit();
> > > > 73:} catch (HibernateException e)
> > > > 74:{
> > > > 75: transaction.rollback();
> > > > 76:}
>
> > > >I'd like to point out that this seems to be an intermittent thing,
> > > >occurring more often when I instantiate many objects quickly (the
> > > >quoted lines 52-72, above, are within a cronstructor in the Object).
>
> > > >Does anyone have any ideas as to what might be happening or where I
> > > >might look for additional information in order to track down this
> > > >bug? Thanks, in advance, for any response on this.
>
> -- _
> __ __ ___ _| | William R. Lorenz <wr...@ex...>
> \ V V / '_| | http://www.clevelandlug.net/ ; "Every revolution was
> \./\./|_| |_| first a thought in one man's mind." - Ralph Waldo Emerson
>
>
|