Luis Sep
Luis Sep

Reputation: 2402

LazyInitializationException when using threads

I keep getting an error when trying to fetch some objects from the database:

org.hibernate.LazyInitializationException (LazyInitializationException.java:19)     - could not initialize proxy - no Session

But I only get the error when using threads. Our application structure method call flow is as:

FacadeImpl.saveRV() -->

@Transactional
@ParallelContext
BusinessImpl.validateEntry() -->

BusinessImpl.getOpMap() -->

@ParallelTask
DAOImpl.getMEFilter()

I get the LazyInitializationException when trying to access a field of the fetched entities, defined as:

// bi-directional many-to-one association to PtoConex
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "IDN_PTO_CONEX", insertable = false, updatable = false)
private PtoConex progTptoConex;

However, if I remove the @ParallelContext annotation from the declaration of BusinessImpl.validateEntry() and @ParallelTask from DAOImpl.getMEFilter(), I don't have any problem.

Upvotes: 1

Views: 1411

Answers (1)

Guillaume
Guillaume

Reputation: 14671

The Hibernate session and the entities loaded from that session are not thread safe. If you try accessing them from different thread you will run into this kind of "weird" exceptions.

Most of the time mixing transactions with concurrency is also a bad idea.

Upvotes: 1

Related Questions