Java / Hibernate - 在只读模式下不允许写入操作错误

时间:2016-04-21 12:51:45

标签: spring hibernate spring-mvc spring-boot

我最近经常遇到一个烦人的例外,经过对谷歌和这个论坛的一些研究后,我仍然没有找到可以解决我问题的答案。

这就是事情 - 有时,我在尝试用hibernate更新或创建一个新对象时遇到以下错误:

我已提及here1here2here3,但无法找到解决方案。

2016-04-21 15:05:12,775 ERROR [stderr] (default task-49) org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

2016-04-21 15:05:12,775 ERROR [stderr] (default task-49)    at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)

2016-04-21 15:05:12,775 ERROR [stderr] (default task-49)    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at com.rss.master.COM.dao.impl.COMDaoImpl.save(COMDaoImpl.java:65)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at com.rss.master.COM.bo.impl.COMBoImpl.save(COMBoImpl.java:39)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at java.lang.reflect.Method.invoke(Method.java:497)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)

2016-04-21 15:05:12,776 ERROR [stderr] (default task-49)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at com.sun.proxy.$Proxy76.save(Unknown Source)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at com.rss.master.COM.bean.COM.save(COM.java:243)

2016-04-21 15:05:12,777 ERROR [stderr] (default task-49)    at com.rss.master.COM.bean.COM.btnSaveActionPerformed(COM.java:428)

applicationContext.xml: -

  <bean id="dataSourceTariff" class="org.apache.commons.dbcp.BasicDataSource">  
       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
        <property name="url" value="jdbc:oracle:thin:@192.168.0.111:1521:XE" /> 
        <property name="username" value="TARIFF"/> 
        <property name="password" value="RSS"/>        
    </bean> 

    <bean id="dataSourceSetting" class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
        <property name="url" value="jdbc:oracle:thin:@192.168.0.111:1521:XE" /> 
        <property name="username" value="Setting"/> 
        <property name="password" value="RSS"/>     
    </bean> 

      <bean id="dataSourceMaster" class="org.apache.commons.dbcp.BasicDataSource">  
       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
        <property name="url" value="jdbc:oracle:thin:@192.168.0.111:1521:XE" /> 
        <property name="username" value="MASTERS"/> 
        <property name="password" value="RSS"/>        
    </bean> 

        <bean id="dataSourceMain" class="com.rss.setting.RoutingDataSource">  
     <property name="targetDataSources">
      <map key-type="com.rss.setting.DbType">

         <entry key="TARIFF" value-ref="dataSourceTariff"/>     
         <entry key="SETTING" value-ref="dataSourceSetting"/>
         <entry key="MASTERS" value-ref="dataSourceMaster"/>
       </map>
     </property>

    </bean>

    <bean id="sessionFactory3"
         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource">
          <ref bean="dataSourceMain"/>
        </property>



        <property name="hibernateProperties">
           <props>
             <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
               <prop key="hibernate.show_sql">false</prop>
           <prop key="hibernate.hbm2ddl.auto">update</prop> 
                <prop key="format_sql">true</prop>

           </props>
        </property>
        </bean>

 <bean id="sessionFactoryMasters"
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSourceMaster"/>
    </property>
    <property name="configLocation">
            <value>classpath:hibernate1.cfg.xml</value>
        </property>


    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
           <prop key="hibernate.show_sql">false</prop>
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
            <prop key="format_sql">true</prop>

       </props>
    </property>




  <property name="mappingResources">
    <list>         
        <value>hbm/Tool.hbm.xml</value>
         <value>TwoCloumnhbm/TwoCloumnSearch.hbm.xml</value>
        <value>CIMHBM/CIM.hbm.xml</value>
</list>
   </property>

</bean>
<bean id="HibernateTemplateMasters" class="org.springframework.orm.hibernate4.HibernateTemplate">
      <property name="sessionFactory" ref="sessionFactoryMasters" />
</bean>

<bean id="sessionFactoryTariff"
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSourceTariff"/>
    </property>
    <property name="configLocation">
            <value>classpath:hibernate2.cfg.xml</value>
        </property>
   <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
           <prop key="hibernate.show_sql">false</prop>
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
            <prop key="format_sql">true</prop>

       </props>
    </property>

  <property name="mappingResources">
    <list>         
           <value>COMHBM/COM.hbm.xml</value>
<!--           <value>TAMHBM/TAM.hbm.xml</value>                 -->
    </list>
   </property>

    </bean>

    <bean id="HibernateTemplateTariff" class="org.springframework.orm.hibernate4.HibernateTemplate">
      <property name="sessionFactory" ref="sessionFactoryTariff" />
    </bean>

    <bean id="sessionFactorySeeting"
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSourceSetting"/>
    </property>
    <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
   <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
           <prop key="hibernate.show_sql">false</prop>
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
            <prop key="format_sql">true</prop>

       </props>
    </property>

  <property name="mappingResources">
    <list>         
           <value>hbm/LoginMaster.hbm.xml</value>             
    </list>
   </property>

    </bean>

LoginMasterBean: -

<bean id="exRateMasterDao" class="com.rss.master.dao.impl.LoginMasterDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactorySeeting"/>
        </property>
    </bean>
    <bean id="loginMasterBo" class="com.rss.master.bo.impl.LoginMasterBoImpl">
        <property name="exRateMasterDao" ref="exRateMasterDao"/>
    </bean>

    <tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactorySeeting" /> 
</bean> 

COM.XML

<bean id="comdao" class="com.rss.master.COM.dao.impl.COMDaoImpl">
     <property name="sessionFactory">
            <ref bean="sessionFactoryTariff"/>
        </property>
        <property name="hibernateTemplate" ref="HibernateTemplateTariff" /> 
</bean>

<bean id="combo" class="com.rss.master.COM.bo.impl.COMBoImpl">
    <property name="comdao" ref="comdao"/> 
</bean>

    <tx:annotation-driven/>
<bean id="transactionManager_COM" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactoryTariff" /> 
</bean> 

首先尝试: -

com.rss.master.COM.dao.impl.save(Country c) {
DbContextHolder.setDbType(DbType.TARIFF); 
getHibernateTemplate().save(c);
}

获取我在错误日志中发布的错误。

第二次尝试: -

com.rss.master.COM.dao.impl.save(Country c) {
DbContextHolder.setDbType(DbType.TARIFF); 
getHibernateTemplate().setCheckWriteOperations(false);
DbContextHolder.setDbType(DbType.TARIFF); 
getHibernateTemplate().save(c);
}

现在没有错误,但数据未保存在表中。似乎没有提交。

第三次尝试: -

com.rss.master.COM.dao.impl.save(Country c) {
DbContextHolder.setDbType(DbType.TARIFF); 
getHibernateTemplate().setCheckWriteOperations(false);
DbContextHolder.setDbType(DbType.TARIFF); 
getHibernateTemplate().save(c);
 getHibernateTemplate().flush();
}

我有像“LoginMasterBean”中的选择数据和JSF中的crating菜单这样的场景,它运行正常。然后从该菜单中将链接许多可以引用不同数据库的屏幕。请有人帮助我解决我的错误以及如何摆脱它。 谢谢

0 个答案:

没有答案