- This topic has 11 replies, 3 voices, and was last updated 17 years, 9 months ago by Riyad Kalla.
-
AuthorPosts
-
denovMemberi’m trying to setup a new project using spring and hibernate. i have an old project that was done in (plain old) eclipse. the big difference on my new myEclipse project is i used the reverse engineering hibernate tool to generate all my Pojos and DAO. When i try to do a save i get this exception. i’ve read a bunch of post on the net about related problems. i still really don’t have a clean understand what why this is and how to resolve it
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.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
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:406)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)root cause
org.springframework.dao.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
org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1098)
org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:616)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:614)
com.autoreturn.dao.AuctionDAO.save(AuctionDAO.java:37)
com.autoreturn.manager.AuctionManagerImpl.createAuction(AuctionManagerImpl.java:27)
com.autoreturn.controller.validator.AuctionCreateValidator.validate(AuctionCreateValidator.java:28)
org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:55)
org.springframework.web.servlet.mvc.BaseCommandController.bindAndValidate(BaseCommandController.java:372)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:248)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)applicatonContext.xml
<bean id=”sessionFactory”
class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<property name=”configLocation”>
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean><bean id=”transactionManager”
class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory” />
</property>
</bean><bean id=”transactionProxyTemplate” abstract=”true”
class=”org.springframework.transaction.interceptor.TransactionProxyFactoryBean”>
<property name=”target”><ref bean=”auctionManager”/></property>
<property name=”transactionManager”>
<ref local=”transactionManager” />
</property>
<property name=”transactionAttributes”>
<props>
<prop key=”*”>PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean><!– ************** Managers ************** –>
<bean id=”auctionManager”
class=”com.autoreturn.manager.AuctionManagerImpl”>
<constructor-arg index=”0″ ref=”AuctionDAO” />
</bean><!– *********** DAOs ************ –>
<bean id=”AuctionDAO” class=”com.autoreturn.dao.AuctionDAO”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory” />
</property>
</bean>spirng-servlet.xml
<bean id=”beanMapping”
class=”org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping”>
<property name=”interceptors”>
<list>
<ref bean=”openSessionInViewInterceptor” />
</list>
</property>
</bean><bean name=”openSessionInViewInterceptor”
class=”org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory” />
</property>
</bean><bean id=”messageSource”
class=”org.springframework.context.support.ResourceBundleMessageSource”>
<property name=”basename”>
<value>messages</value>
</property>
</bean>thanks,
deno
Riyad KallaMemberHmm, what does your hibernate.cfg.xml file look like?
denovMember@support-rkalla wrote:
Hmm, what does your hibernate.cfg.xml file look like?
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">dvichas</property> <property name="connection.url"> jdbc:sqlserver://localhost:1433;database=auction </property> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <property name="myeclipse.connection.profile"> localhost </property> <property name="connection.password">dvichas</property> <property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver </property> <property name="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </property> <mapping resource="com/autoreturn/model/Vehicle.hbm.xml" /> <mapping resource="com/autoreturn/model/AttendeeType.hbm.xml" /> <mapping resource="com/autoreturn/model/AttendeeStatus.hbm.xml" /> <mapping resource="com/autoreturn/model/PaymentType.hbm.xml" /> <mapping resource="com/autoreturn/model/Deposit.hbm.xml" /> <mapping resource="com/autoreturn/model/Purchase.hbm.xml" /> <mapping resource="com/autoreturn/model/LicenseType.hbm.xml" /> <mapping resource="com/autoreturn/model/Address.hbm.xml" /> <mapping resource="com/autoreturn/model/AuctionAttendees.hbm.xml" /> <mapping resource="com/autoreturn/model/Attendee.hbm.xml" /> <mapping resource="com/autoreturn/model/Auction.hbm.xml" /> </session-factory> </hibernate-configuration>
Haris PecoMemberThe OpenSessionInViewInterceptor will by default not flush the Hibernate Session.You can try set the “flushMode” of this interceptor to FLUSH_AUTO
(property flushMode in interceptor’s bean)Regards,
denovMember@support-snpe wrote:
The OpenSessionInViewInterceptor will by default not flush the Hibernate Session.You can try set the “flushMode” of this interceptor to FLUSH_AUTO
(property flushMode in interceptor’s bean)this doesn’t work. i added to my OpenSessionInViewInterceptor bean
<property name="flushModeName"> <value>FLUSH_AUTO</value> </property>
on another a project we didn’t have to set the flush mode for OpenSessionInViewInterceptor. another developer (that’s no longer here) did the initial spring and hibernate setup. we aren’t using a transaction proxy either. the only real difference i see is that we use session.save() vs. getHibernateTemplate().save().
denovMemberone other thing;
putting a getHibernateTemplate().flush() after the getHibernateTemplate().save(transientInstance) statement does nothing.
the hibernate reserve engineering tool created a HibernateSessionFactory class. Does the reserve engineering tool put this to use anywhere? i’m i suppose to be using it?
Riyad KallaMember<property name=”flushModeName”>
<value>FLUSH_AUTO</value>
</property>I believe the property is “flushMode”, try that and see if it works.
denovMember@support-rkalla wrote:
<property name=”flushModeName”>
<value>FLUSH_AUTO</value>
</property>I believe the property is “flushMode”, try that and see if it works.
what would be the correct value for this be?
denovMemberwait.. flushModeName should work the same as flushMode. flushModeName sets it by the constant. flushMode sets it by the int value of the constant.
http://www.springframework.org/docs/api/org/springframework/orm/hibernate3/HibernateAccessor.html#setFlushModeName(java.lang.String)
http://www.springframework.org/docs/api/org/springframework/orm/hibernate3/HibernateAccessor.html#setFlushMode(int)
Haris PecoMemberDeno,
FlushMode is your problem.You can try with flushMode (it is possible that spring have some bug) or override OpenSessionInViewInterceptor and set flush mode in the getSession or openSession method.You can try hibernate operations using HibernateTemplaet#execute directly as well.
Regards
denovMemberok i figured it out. i’ve seen so many posts about setting the flush Mode in the OpenSessionInViewInterceptor and none of them really made much sense to why. i personally think this is just wrong. to resolve this problem you need to wrap all hibernate calls in a transaction. i found this blog after i figure it out from reading spring and hibernate docs.
http://nerdnotes.wordpress.com/2007/03/30/basic-transactions-in-spring-using-transactionproxyfactorybean/ it shows one one to do this. my method is a little different.
<!-- ************** Hibernate ************** --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="transactionProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- ************** Managers ************** --> <bean id="AuctionManager" parent="transactionProxyTemplate"> <property name="target"> <bean class="com.autoreturn.manager.AuctionManagerImpl"> <constructor-arg index="0" ref="AuctionDAO" /> </bean> </property> </bean>
Riyad KallaMemberdenov,
Thank you *very* much for posting all your findings for other folks running into the same issue. I personally haven’t run into this before so I was at a loss as to what had to be done so this information is very helpful. -
AuthorPosts