关于spring +ibatis 数据无法回滚问题

zhongxueye 2015-09-14 11:58:04
在做一个项目,插入用户的时候,取用户的id值插入艺术家,艺术家表字段有些值不能为空, 然后用户插入成功,但是插入下一条数据的时候,报.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/linkcare/chengart/model/Artist_SqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the artist.insertArtist-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'birth_year' cannot be null,就是数据库的表不能某些字段不能为空,配置文件如下<bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:SqlMap-Config.xml</value>
</property>
<property name="dataSource" ref="dbcpDataSource" />
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMap" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dbcpDataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
无论是编程式跟注解都不成功,我的想法是不是跟表的设计不能为空有影响,抱歉,分数不多望解答
...全文
402 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongxueye 2015-09-15
  • 打赏
  • 举报
回复
请看17楼到18楼,同时感谢华为总监跟版主,,
zhongxueye 2015-09-15
  • 打赏
  • 举报
回复
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <mvc:annotation-driven /> <!--<context:component-scan base-package="com.linkcare.chengart" />--> <context:component-scan base-package="com.linkcare.chengart" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <!-- 默认首页 <mvc:view-controller path="/" view-name="redirect:/toLogin/ " /> --><!-- 静态文件访问 --> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:resources mapping="/web/**" location="/web/" /> <!--拦截器 --><!-- <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.linkcare.chengart.interceptor.InterceptorController" /> </mvc:interceptor> </mvc:interceptors> --><!--文件下载--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- set the max upload size100MB --> <!--<property name="defaultEncoding" value="utf-8"></property> --><property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> <!-- 启动spring mvc的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> </beans>
zhongxueye 2015-09-15
  • 打赏
  • 举报
回复
昨天的问题解决呢,我把配置文件发下,感谢以前华为老大三分钟解决呢我三天解决不了的问题,下面是配置文件,希望对以后出现此问题的人有启示,aop代理换成了gclib代理<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config/> <!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 --><!-- <context:component-scan base-package="com.linkcare.chengart"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> --> <context:component-scan base-package="com.linkcare.chengart" > <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> --></context:component-scan> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- DBCP DataSource数据源 --> <bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="username" value="${user}"/> <property name="password" value="${password}"/> <property name="driverClassName" value="${driverClass}"/> <property name="url" value="${jdbcUrl}"/> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"></property> </bean> <bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:SqlMap-Config.xml</value> </property> <property name="dataSource" ref="dbcpDataSource" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMap" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dbcpDataSource"/> </bean> <!--<tx:annotation-driven transaction-manager="transactionManager"/> --><!-- SpringMVC L10n --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"> <value>messages</value> </property> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/> <tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="vote*" rollback-for="Exception" /> <tx:method name="post*" rollback-for="Exception" /> <tx:method name="*" read-only="true" rollback-for="Exception" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.linkcare.chengart.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> <aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
Defonds 2015-09-14
  • 打赏
  • 举报
回复
艺术家表的存储引擎是什么? MyISAM 是不支持事务滴
Defonds 2015-09-14
  • 打赏
  • 举报
回复
<!-- class annotation related... start -->  
    <context:component-scan base-package="com.defonds.bdp.*.service" />  
    <!-- class annotation related... end -->  
    <context:annotation-config />  
      
    <!-- transaction config related... start -->  
    <tx:annotation-driven transaction-manager="transactionManager" />  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
    <!-- transaction config related... end -->
类似于这样配一下试试
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
引用 13 楼 zhongxueye 的回复:
谢谢版主,我也不纠结呢,继续研究,研究出来写篇博客分享下,谢谢。
Defonds 2015-09-14
  • 打赏
  • 举报
回复
http://blog.csdn.net/defonds/article/details/48137969 这里有个 spring 整合 mybatis 事务管理的例子,你可以参考这个配一下看看?
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
引用 11 楼 defonds 的回复:
此外,你的 applicationContext 配置里没有看到 service 类的 component-scan 配置
一种是声明型一种是编码型是吧,我这样配置的,版主你看下
Defonds 2015-09-14
  • 打赏
  • 举报
回复
此外,你的 applicationContext 配置里没有看到 service 类的 component-scan 配置
Defonds 2015-09-14
  • 打赏
  • 举报
回复
但是上边的截图里,注释里好像有捕捉异常的代码。 你把 class 里的 service 类反编译一下,看看是最新的吗?怀疑走的还是注释前的代码~
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
引用 8 楼 defonds 的回复:
业务代码里不要捕捉异常。
不然 spring 不知道你抛了异常了,也不回滚的
我没有经行捕获哦
Defonds 2015-09-14
  • 打赏
  • 举报
回复
业务代码里不要捕捉异常。 不然 spring 不知道你抛了异常了,也不回滚的
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
引用 3 楼 defonds 的回复:
换成这个注释试试:
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
你好,版主,我加上这个注解还是没什么效果。就是报错是那个字段不能为空
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
引用 4 楼 xl61624463 的回复:
你看看是不是sequence没找到 或者SQL本身拼错了
这样描述下,上面可能我说的迷糊,现在有用户跟艺术家两张表,艺术家新增:例如姓名电话等等是存到用户表的,其他信息是存艺术家表的,艺术家表的字段很多都设置不能为空,所以他执行到艺术家的dao方法时,有艺术家某字段不能为空的异常,用户数据增加成功,艺术家失败没有数据,关键用户数据不回滚,sql如果对应的值是正确的,艺术家跟用户数据都能添加
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
要表结构我是设计他不能为空,所以执行sql的时候检查到数据库的限制跑出sqlexception异常,不执行数据回滚呢,公司没大牛,没人请教
諵柯 2015-09-14
  • 打赏
  • 举报
回复
你看看是不是sequence没找到 或者SQL本身拼错了
Defonds 2015-09-14
  • 打赏
  • 举报
回复
换成这个注释试试:
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
zhongxueye 2015-09-14
  • 打赏
  • 举报
回复
引用 1 楼 defonds 的回复:
艺术家表的存储引擎是什么? MyISAM 是不支持事务滴
你好,艺术家表跟用户表都是InnoDB

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧