Note that there are some explanatory texts on larger screens.

plurals
  1. POJava / Hibernate - Write operations are not allowed in read-only mode
    primarykey
    data
    text
    <p>I've been having an annoying exception a lot lately, and after some research on Google and this forum I still haven't found an answer that could solve my problem.</p> <p>Here's the thing - sometimes, I get the following error when trying to update or create a new object with hibernate:</p> <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186) at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694) </code></pre> <p>What is really strange is that, sometimes when updating an object with the method <code>getHibernateTemplate().saveOrUpdate(object);</code> it will work, but sometimes with the same object and by calling the same method it doesn't work, but it seems to depend on how I get the object in the first place.</p> <p>Example: let's say I have a table with 3 fields: id, type, length. What can happen is that, if I get the object by the id and update the length, then it will work. If I get it by the type and update the length, then it won't work. So what I've been doing so far to avoid the problem is to fetch the object the method that does not cause a problem later, but this is becoming more and more annoying to try and find a way that works.</p> <p>Also, now I have this exception when trying to create an object (but not all of them, just on one specific table), and can't find a way for a workaround. And I tried to add <code>@Transactional(readOnly = false)</code> in the transaction but it didn't change anything, and displaying the mode was saying that I wasn't in read-only anyway.</p> <p>Any suggestions?</p> <p>Edit 26th of July: here's some configuration related to hibernate</p> <pre><code>&lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="jdbc.fetch_size"&gt;20&lt;/prop&gt; &lt;prop key="jdbc.batch_size"&gt;25&lt;/prop&gt; &lt;prop key="cglib.use_reflection_optimizer"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt; &lt;prop key="connection.autoReconnect"&gt;true&lt;/prop&gt; &lt;prop key="connection.autoReconnectForPools"&gt;true&lt;/prop&gt; &lt;prop key="connection.is-connection-validation-required"&gt;true&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; </code></pre> <p>also, if it can help</p> <pre><code>&lt;property name="transactionAttributes"&gt; &lt;props&gt; &lt;prop key="get*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt; &lt;prop key="find*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt; &lt;prop key="execute*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt; &lt;prop key="add*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt; &lt;prop key="create*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt; &lt;prop key="update*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt; &lt;prop key="delete*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; </code></pre> <p>Edit 31st of August: The relevant code in my Class that extends <code>HibernateDaoSupport</code>, to save the objects is:</p> <pre><code>public void createObject(Object persisObj) { getHibernateTemplate().save(persisObj); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload