PSQLException: Large Objects may not be used in auto-commit mode

♀尐吖头ヾ 提交于 2019-12-04 14:15:08

I cannot tell you how this is done in Hibernate, but opening and reading/writing a large object have to happen within the same database transaction.

Disabling autocommit mode should do the trick, maybe you did something wrong.

But may I suggest that you do not use large objects at all?
Usually it is much easier to use the bytea PostgreSQL data type, which can contain data up to 1GB of size. Unless you store and retrieve the data in chunks, large objects don't offer any advantage, and I doubt that you can fully exploit large object functionality with an ORM anyway.

I had the same problem and resolved it through a change in my JBOSS configuration file (standalone.xml or domain.xml).

Analysing the problem first I saw that auto commit was still true by logging

entityManager.unwrap(SessionImpl.class).connection().getAutoCommit()

I tried setting it to false with

entityManager.unwrap(SessionImpl.class).connection().setAutoCommit(false)

but this resulted in the exception "You cannot set autocommit during a managed transaction".

Researching for the exception message I found this article that explains why auto commit was always true.

We were using a datasource configured like this:

<datasource jta="true"  . . . >

The problem is that in a JTA datasource the auto-commit setting is always true and it can not be changed. I changed it to an xa-datasource and the problem did not occur anymore.

<xa-datasource . . . .>

Also em.unwrap(SessionImpl.class).connection().getAutoCommit() finally returned false.

To make it work I also had to set the parameter max_prepared_transaction to 500 in postgres.conf in my Postgres database to allow multiple database connection/connection pooling:

max_prepared_transactions = 500     # zero disables the feature
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!