title: HIbernate InvalidDataAccessApiUsageException - read-only mode
body: <p>Summary: the Exception is telling me that the transaction is read-only; a debug println seems to indicate that I'm not in read-only mode.</p>
  
  <p>Classes edited for internet publishing - sorry if I mistyped something but this is the jist of the code giving me problems.  saveOrUpdate works when called on other object types but not on this one.  I added the println to saveOrUpdate as I was debugging.  I didn't write the abstract class, I'm just trying to use it (and now debug it).</p>
  
  <p>Relevant output below code.  Not sure where to go from here.</p>
  
  <p>Update after investigation:
  I have also been in the middle of doing some updates to the spring config and a coworker pointed out that one method I called updateAParameter from was using spring in one way, and the broken method was using it in another way.  Unfortunately the broken way is the way I was trying to get to.</p>
  
  <p>So the problem as I now understand it is that if I instantiate the DataObjectDAOImpl "manually" in a method by getting a bean, then it allows me to write back to Hibernate correctly.  If I use spring to set a class variable for that bean so I don't have to instantiate it in every method, then the InvalidDataAccessApiUsageException occurs when I access a method that tries to write to Hibernate, despite the fact that it reports not being in read-only mode.  My coworker had a theory on this topic but I didn't understand what he was trying to say - something about extracting an interface from the SampleClass.</p>
  
  <pre><code>// Old way that works.
  public class SampleClass {
  public void someMethod {
  ApplicationContext ac = ApplicationContextFactory.getApplicationContext();
  DataObjectDAOImpl dodi = ((DataObjectDAOImpl) ac.getBean("dodi"));
  //this works
  dodi.updateAParameter("foo", exampleDataObject);
  }
  }
  
  //New way that doesn't work but I would like it to.
  public class SampleClass {
  private DataObjectDAOImpl dodi = null;
  //'dodi' has getter and setter methods that I am not pasting here for simplicity
  public void someMethod {    
  //causes Exception
  dodi.updateAParameter("foo", exampleDataObject);
  }
  }
  </code></pre>
  
  <p>and here is relevant bean from the spring config</p>
  
  <pre><code>&lt;bean id="sampleclass" class="com.service.SampleClass" scope="prototype"&gt;
      &lt;property name="dodi" ref="doDAOimpl"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>here is the DAOImpl which is the same for the old and new way</p>
  
  <pre><code>public class DataObjectDAOImpl extends AbstractSpringDaoStuff {
  
   ...
  
   public void updateAParameter(String parameter, DataObject do) {
    do.setAParameter(parameter);
    super.saveOrUpdate(do);
   }
  
  }
  
  
  public abstract class AbstractSpringDaoStuff extends HibernateDaoSupport {
  
     ...
  
      @Transactional(readOnly=false)
       protected void saveOrUpdate(Object obj) {
       System.out.println ( "Read-only?: " + TransactionSynchronizationManager.isCurrentTransactionReadOnly () );
  
           getHibernateTemplate().saveOrUpdate(obj);
       }        
  }
  </code></pre>
  
  <p>Output from the app server:</p>
  
  <pre><code>     [java] Read-only?: false
       [java] - Method execution failed:
       [java] 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.
       [java]     at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186)
       [java]     at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:750)
       [java]     at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
       [java]     at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
       [java]     at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:748)
       [java]     at com.est.dao.AbstractSpringDaoStuff.saveOrUpdate(AbstractSpringDaoMDA.java:24)
  etc
  </code></pre>