解决方案 »
- 如何解决拥塞问题
- Intra-mart和SSH2框架哪个好啊?
- 如何判别线程池里的任务全部已经完成?
- Spring + iBatis 中两个数据源怎么做事务控制
- Hibernate中实体类用Serializable和不用Serializable有什么区别???
- ★★★JSP网站发布详细流程,结贴必送分!★★★
- 用过portlet的请进!(100分求救,好心人帮顶一下,有分)
- 请大家帮忙看下这逻辑有错吗
- STRUTS登陆跳转的问题
- 关于数据库数据迁移问题,求帮助
- JPA通过外键查询时报错:could not resolve property
- 搭建了个SpringMvc的框架,使用Controller跳转时可以跳转到jsp类型的页面,html的确报404.
Hibernate4已经是全部用了自己的事务处理框架,Spring集成事务层必须打开事务放行。
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<tx:method name="create*" propagation="REQUIRED" read-only="false" />
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
<tx:method name="reg*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(com.eai.serviceImpl.*.*(..))"
order="1"/>
</aop:config>但是启动项目的时候报错,java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext。
不知道我离成功越来越近还是越来越远了。
问题就如异常所述,是flushmode属性问题。
对数据库的操作都属于事务的操作,而数据库默认是读已提交 read-only="true"形式
当读写操作时会调用DAO方法,而操作DAO的核心业务是Service,即 Spring,然而Spring
要对调用DAO的方法加以控制,所以就产生了切面(advice)
新增配置如下:
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<tx:method name="create*" propagation="REQUIRED" read-only="false" />
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
//实际只用到reg这个连接点。
<tx:method name="reg*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* com.easi.serviceImpl.*.*(..)))"
order="1"/>
</aop:config>报错需要导入aop的一些包,根据错误提示缺什么导什么就行了。虽然异常解决了,但是我觉得这种方式不好,其实根本原因就是readonly=fale所以不能写入数据源。只要单独设置数据源readonly属性即可,不需要引入切面。当然引入切面好处是以后逻辑业务多了,spring可以可以通过切面连接点很好的保护数据源。