We’re developing a project in JaveEE6, using EJB3 beans and JPA2 annotations.
We have some stateful EJB3 beans that use extended persistence context in order to display the database entities to the front (through some interfaces, removing the need of DTO).
The typical use is something like:
- all methods are without transaction, as we don’t want to immediately commit user modification
- through a non-transactional method, we’re loading the entity, attached to the extended context
- only a save method is transactional: after checking the user data, the entity is then committed and persisted in database.
With a MySQL database, everything works just fine.
Alas, on Postgres, a problem occurs with @Lob field loaded in non-transactional methods. JDBC seems to forbid Lob access outside transaction, throwing a:
org.hibernate.exception.GenericJDBCException: Large Objects may not be used in auto-commit mode
As a stackoverflower pointed, a Lob can be in multiple records, so it needs a transaction to maintain consistency.
Set autocommit to true in persistence.xml doesn’t work at all, and also should not be done.
I cannot make the method transactional, as we don’t want to commit anything at the end of the call. So, does anyone know how can I simply access the Lob?
A hack solution we imagine would be to move the Lob in another entity, then add a transactional method that read the Lob content so we can use it. Quite dirty I think…
For some architectural reasons, we chose to set the
Lobfield in anotherEntityand read/write it through a@Statelessbean.The entity:
The service:
And a class that directly contained the lob but no more:
The entity doesn’t have the
LobEntityitself in order to avoir developers to try to access it like fools: if we want the content from a non-transactional context, we’re using theLobService. And when we want to save the edited content, we’re also using the same bean.