解决 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值