解决 Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session in

采用spring的事务声明 

     <bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
           abstract="true"> 
         <property name="transactionManager" ref="transactionManager"/> 
         <property name="proxyTargetClass" value="true"/> 
         <property name="transactionAttributes"> 
             <props> 
                 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> 
                 <prop key="save*">PROPAGATION_REQUIRED</prop> 
                 <prop key="add*">PROPAGATION_REQUIRED</prop> 
                 <prop key="update*">PROPAGATION_REQUIRED</prop> 
                 <prop key="remove*">PROPAGATION_REQUIRED</prop> 
             </props> 
         </property> 
     </bean> 


     <bean id="userService" parent="baseTransaction"> 
         <property name="target"> 
             <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/> 
         </property> 
     </bean> 


使用spring  aop,你写的方法就应用了事务

容易报错  classNotFound:org.aopalliance.aop.Advice

准备工作:
1.下载spring
下载后的压缩包为spring-3.2.0.M2-dist.zip,为spring3.2版本.
2.下载commons-logging.jar文件,spring依赖这个jar,否则运行异常,用于日志输出  下载地址:地址:http://commons.apache.org/logging/download_logging.cgi。
3.aspectj.jar  aspectjweaver.jar aopalliance.jar,是切面开发需要的包。我下到的是aspectj-1.6.10.jar,aspectjweaver-1.5.3.jar,aopalliance.jar
4.cglib-nodep.jar  ,cglib用于无实现接口对象的代理对象生成。我下到的是cglib-nodep-2.1_3.jar

可在网站http://search.maven.org/#search%7Cga%7C1%7Caopalliance搜索这些jar

 <!-- 配置事务管理器 -->
    <bean id="transactionmanager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 配置传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionmanager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="batch*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置事务关联包 -->
    <aop:config>
        <aop:pointcut id="allServiceMethod"
            expression="execution(* com.jndi.test.Service*.*(..))" />
        <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
    </aop:config


你可能感兴趣的:(解决 Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session in)