PDA

View Full Version : InvalidDataAccessApiUsageException when saving objects (using OSIV)


tbroyer
03-28-2007, 02:54 PM
Hi,

I'm having an InvalidDataAccessApiUsageException ("Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition") when I try to save or update an entity.
I'm using an OpenSessionInViewModule in an ASP.NET 2.0 application.

If I use HibernateTemplate.Execute and call SaveOrUpdate on the ISession, no exception is thrown but as the session is never flushed (except if I explicitly call ISession.Flush) nothing is stored in the database.

I know the OpenSessionInViewModule is setting the session's FlushMode to FlushMode.Never, and comments within the code say I could create a derived class and override this behavior.

I find it a bit odd, so I'm wondering what's the expected "coding style":
should I use HibernateTemplate.Execute and call SaveOrUpdate on the ISession avec turning it into FlushMode.Auto (rather than calling HibernateTemplate.SaveOrUpdate)?
should I use HibernateTemplate.Execute an explicitly call ISession.Flush() after the call to ISession.SaveOrUpdate?
should I use a derived OSIV overriding the default FlushMode?Thanks in advance

boriska
03-29-2007, 09:49 AM
Hello,

try to use transactions.

You have e.g. to mark your service methods with [transaction] and
to involve DeclarativeServicesAttributeDriven.xml. That worked for me.

Nightly Spring.Hibernate builds contain the Nothwind example; The Spring's QuickStart to transaction topic can help too.

Regards,
Boris

tbroyer
04-03-2007, 02:29 PM
Hello,

try to use transactions.

You have e.g. to mark your service methods with [transaction] and
to involve DeclarativeServicesAttributeDriven.xml. That worked for me.


Doh! I already had the TransactionInterceptor but was testing on a method whereas the [Transaction] attribute was set on another one

Stupid, stupid stupid! ;)

tbroyer
04-04-2007, 09:19 AM
Stupid, stupid stupid! ;)

Actually not !!!

If I do not explicitly call session.Flush(), the session is NEVER flushed, even within a transaction!

Here's what I have in my config file:

<object name="_transactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="SessionFactory" ref="sessionFactory" />
</object>

<object name="transactionInterceptor" type="Spring.Transaction.Interceptor.TransactionIntercep tor, Spring.Data">
<property name="TransactionManager" ref="_transactionManager" />
<property name="TransactionAttributeSource">
<object type="Spring.Transaction.Interceptor.AttributesTransacti onAttributeSource, Spring.Data" />
</property>
</object>

<object name="BaseProxiedService" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop" abstract="true">
<property name="InterceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</object>

<object name="ConsumersService" parent="BaseProxiedService">
<property name="Target" ref="_consumersService" />
</object>
And the method I'm calling in ConsumersService is marked with [Transaction].

Could there be a problem if not every method is marked with [Transaction] (actually, only methods used to SaveOrUpdate things are "transactioned")?