HIbernate InvalidDataAccessApiUsageException - read-only mode

Summary: An exception tells me that a transaction is read-only; The debug println seems to indicate that I'm not in read-only mode.

Classes edited for publishing on the Internet - sorry if I suspect something, but this is code craziness giving me problems. saveOrUpdate works when calling other types of objects, but not on this one. I added println to save OrUpdate since I was debugging. I did not write an abstract class, I'm just trying to use it (and now I am debugging it).

The corresponding output is below the code. Not sure where to go from here.

Update after investigation: I was also in the middle of doing some spring configuration updates, and a colleague pointed out that one method, which I called updateAParameter, used with spring in one way, and the broken method used it differently. Unfortunately, the broken way is what I was trying to get to.

So, the problem that I now understand is that if I create an instance of DataObjectDAOImpl manually in the method, getting a bean, then it allows me to write back correctly to Hibernate. If I use spring to set a class variable for this bean, so I don’t need to create it in each method, then an InvalidDataAccessApiUsageException exception occurs when I call a method that tries to write to Hibernate, even though it reports are not in mode only for reading. My colleague had a theory on this topic, but I did not understand what he was trying to say - something about extracting an interface from SampleClass.

// 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); } } 

and here bean from spring config matters

 <bean id="sampleclass" class="com.service.SampleClass" scope="prototype"> <property name="dodi" ref="doDAOimpl"/> </bean> 

here is DAOImpl, which is the same for the old and new way

 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); } } 

Exit from the application server:

  [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 
+6
java spring orm hibernate readonly
source share
1 answer

The only possible problem that I see here is that you are calling the @Transactional method from the same bean. I'm not sure how this could be related to your exception, but since Spring declarative transaction management is implemented through proxy-based AOP, this means that this annotation does not take effect.

See also:

+3
source share

All Articles