`
my_java_life
  • 浏览: 143541 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

关于Spring + ibatis事务配置,以及出错分析

阅读更多

1.最近在忙于做一个项目,用到EXTJS4.0 + spring + ibatis,在事务回滚的时候出现了问题?搞了一天才解决

 

解析步骤:

(1)由于ibatis引擎有多个,有的支持事务,有的不支持,查询如下:


只有InnoDB引擎支持事务控制,所以需要修改数据库引擎,有两种方法:

A、我们在mysql的初始化文件my.ini里设置mysql数据库默认引擎;my.ini文件中,在[mysqld]下加上
default-storage-engine=InnoDB // 设置默认引擎为InnoDB。

 

B、直接在表后面加上 ENGINE=InnoDB也可以,如下:

DROP TABLE IF EXISTS `forgetwork`;

CREATE TABLE `forgetwork` (
  `forgetID` int(11) NOT NULL AUTO_INCREMENT,
  `employeeID` int(11) NOT NULL,
  `forgetDate` date NOT NULL,
  PRIMARY KEY (`forgetID`),
  UNIQUE KEY `employeeID_Date` (`employeeID`,`forgetDate`)
) ENGINE=InnoDB AUTO_INCREMENT=353 DEFAULT CHARSET=utf8;

 

(2)结果修改以后还不行,于是怀疑是配置的问题,加过发现切入点少配置了一层:

修改前:

<aop:config>
		<aop:pointcut id="servicesPointcut"
			expression="execution(* com.ml.org.*.service.impl.*


(..))"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesPointcut" />
	</aop:config>

 修改后:

<aop:config>
		<aop:pointcut id="servicesPointcut"
			expression="execution(* com.ml.org.*.service.impl.*.*


(..))"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesPointcut" />
	</aop:config>

 

(3)结果还是不行,不过这次是报错了,事务已经启动了,如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workManageService' defined in ServletContext resource [/WEB-INF/classes/spring/applicationContext-service.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:478)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)

 

这个错误比较明显,缺少CGLIB包,于是下载了个:cglib-2.2.jar。

 

(4)结果还是报错,如下:

 

 org.objectweb.asm下面的类找不到ClassNotFound



   asm-2.2.2.jar

   asm-commons-2.2.2.jar
   asm-util-2.2.2.jar

 

在这里得到一个消息:(转)

在使用Spring的AOP编程时,会用到这几个lib:
asm-2.2.2.jar
asm-commons-2.2.2.jar
asm-util-2.2.2.jar
Hibernate使用如果lib:
asm.jar
asm-attrs.jar
其中asm-2.2.2.jar与asm.jar存在类上的冲突!!!
使用其中之一或两者都使用,可能会出现如下错误:
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
java.lang.NoClassDefFoundError: org/objectweb/asm/commons/EmptyVisitor
java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit。。。。。。
解决办法是:
1.去掉类路径上的关于Hibernate的3个lib
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
2.加入Spring中的以下4个lib
asm-2.2.2.jar
asm-commons-2.2.2.jar
asm-util-2.2.2.jar
cglib-nodep-2.1_3.jar

 

(5)下载后,添加到classpath下,还是报错,如下:

 

Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm 

 

原因:是cglib的版本出现问题。

使用 cglib-nodep-2.2.jar 替换掉 cglib-2.2.jar 这个文件。
 
(6)终于解决问题了。
 
 注意:默认Spring仅支持RuntimeException回滚操作;
           在后续的项目中发现不管怎么抛出自定义异常(继承Exception)都不会滚,无奈只能使自定义异常
           继承RuntimeException,才得到解决。

 

我的事务配置文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="clientFactory" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="configLocation">
			<value>classpath:ibatis/config.xml</value>
		</property>
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 管理事务操作 -->

	<aop:config>
		<aop:pointcut id="servicesPointcut"
			expression="execution(* com.ml.org.*.service.impl.*.*(..))"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesPointcut" />
	</aop:config>
	<!-- 事务控制 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!--  
			<tx:method name="create*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="mod*" propagation="REQUIRED" />
			<tx:method name="update" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
		    -->
			<tx:method name="get*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="*" rollback-for="BMSException"/>



		</tx:attributes>
	</tx:advice>
</beans>

 

 

 

  • 大小: 48.1 KB
0
0
分享到:
评论
1 楼 myali88 2012-02-20  
能把你项目的截个图看看吗?

相关推荐

Global site tag (gtag.js) - Google Analytics