解决方案 »
- 求助select语句
- Struts2.0 静态访问无效
- 同一个程序点过三到四次的时候就回很慢,使得是ORACLE数据库
- Java读取Access中的多国语言
- ehcache配置问题?
- 为什么xml文件被认为是HTML?WebLogic还是浏览器的问题?请进……
- JVM Crash 异常退出:SIGSEGV (0xb)问题
- 一道网易笔试题,关于java中的构造函数调用问题
- HIBERNATE 下怎么样调用ORACLE 的存储过程???
- 怎样初始化InitialContext?
- 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可以可以通过切面连接点很好的保护数据源。