title: PSQLException: Large Objects may not be used in auto-commit mode
body: <p>I am using WildFly 10, Java EE, JPA, and Hibernate. Recently I migrated my application from MySQL to PostgreSQL. While using MySQL, I would store images in my entities using:</p>
  
  <pre><code>@Lob
  @Basic(fetch = FetchType.LAZY)
  private byte[] image;
  </code></pre>
  
  <p>This worked great, and MySQL used a <code>LONGBLOB</code> to store the data.</p>
  
  <p>After switching to PostgreSQL, the column type is <code>OID</code>, and I receive this error when persisting the image:</p>
  
  <pre><code>Caused by: org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode.
  at org.postgresql.largeobject.LargeObjectManager.createLO(LargeObjectManager.java:308)
  at org.postgresql.largeobject.LargeObjectManager.createLO(LargeObjectManager.java:296)
  at org.postgresql.jdbc.PgPreparedStatement.createBlob(PgPreparedStatement.java:1202)
  at org.postgresql.jdbc.PgPreparedStatement.setBlob(PgPreparedStatement.java:1243)
  at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setBlob(WrappedPreparedStatement.java:1157)
  at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$4$1.doBind(BlobTypeDescriptor.java:112)
  at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73)
  at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:257)
  at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:252)
  at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:39)
  at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2598)
  at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2883)
  ... 131 more
  </code></pre>
  
  <p>I am inserting in this way:</p>
  
  <pre><code>@PersistenceContext
  EntityManager entityManager;
  
  ...
  
  //Simple insert method...
  this.entityManager.persist(entity);
  </code></pre>
  
  <p>And this is my <code>persistence.xml</code>:</p>
  
  <pre><code>&lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
      &lt;persistence-unit name="MyApp" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;java:jboss/datasources/PostgreSQLDS&lt;/jta-data-source&gt;
          &lt;shared-cache-mode&gt;DISABLE_SELECTIVE&lt;/shared-cache-mode&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.enable_lazy_load_no_trans" value="true"/&gt;
              &lt;property name="hibernate.cache.use_second_level_cache"
                        value="true"/&gt;
              &lt;property name="hibernate.cache.use_query_cache" value="true"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I have looked at multiple other questions regarding this exception, however I have been unable to solve the problem. </p>
  
  <p><a href="https://stackoverflow.com/a/17479964/2364405">One suggestion</a> was to create an entity solely for storing the data and use a <code>@OneToOne</code> relationship with it so that the entity storing data could be eagerly fetched, however this did not change the results in any way.</p>
  
  <p>Another suggestion was to disable Hibernate's auto-commit with</p>
  
  <pre><code>&lt;property name="hibernate.connection.autocommit" value="false"/&gt;
  </code></pre>
  
  <p>...which also did nothing.</p>
  
  <p>I am completely lost as to why this will not work. Seeming as how this does not appear to be a very common exception, I feel like I am approaching the task of storing image data in the completely wrong way. What can I do to fix this, or how should I be storing the data?</p>