Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring事务回滚错误_Java_Database_Spring_Jakarta Ee - Fatal编程技术网

Java Spring事务回滚错误

Java Spring事务回滚错误,java,database,spring,jakarta-ee,Java,Database,Spring,Jakarta Ee,我有以下方法,在调用callBothMethodInvocation()方法后,我希望在执行insertUser()时插入的用户将回滚,因为insertUserWithException()发生异常。然而,当我检查数据库用户表时,表中存在实体。 我错过了什么? 我发现了类似的问题,但找不到确切的解决办法 @Transactional(rollbackFor=Exception.class) private void callBothMethodInvocation() throws Ex

我有以下方法,在调用
callBothMethodInvocation()
方法后,我希望在执行
insertUser()
时插入的用户将回滚,因为insertUserWithException()发生异常。然而,当我检查数据库用户表时,表中存在实体。 我错过了什么? 我发现了类似的问题,但找不到确切的解决办法

@Transactional(rollbackFor=Exception.class)    
private void callBothMethodInvocation() throws Exception {
    insertUser();
    insertUserWithException();      
}


public void insertUserWithException() throws Exception{
    throw new Exception("fake exception for rollback");
}

@Transactional(rollbackFor=Exception.class) 
public void insertUser(){
    int id = (int) (System.currentTimeMillis()%1000);
    System.out.println("Insert user id:"+id);
    User user = new User(id,"Test",22);
    testDao.insertUser(user);
}
弹簧配置:

<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" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"   
    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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd"
    default-lazy-init="false">


    <context:component-scan base-package="com.mkyong" />

    <context:property-placeholder location="config.properties" />

    <bean id="helloBean" class="com.mkyong.common.HelloWorld">
        <property name="name" value="Ahmet" />
    </bean>

    <bean id="person" class="com.mkyong.common.Person">
        <constructor-arg type="java.lang.String" value="DefaultName"/>
        <constructor-arg type="int" value="30"/>
    </bean>

    <bean id="jmxAdapter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="beans">
            <map>
                <entry key="SPRING:Name=TestRun">
                    <ref bean="jmxService" />
                </entry>
            </map>
        </property>
        <!-- managemethods property starts -->
        <property name="assembler">
            <bean
                class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
                <property name="managedInterfaces">
                    <value>com.mkyong.jmx.JmxCoreComands</value>
                </property>
            </bean>
        </property>
        <!-- managemethods property ends -->
    </bean>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>      
    </bean>

    <!-- Cache Mekanizmasi -->  
    <ehcache:annotation-driven />
    <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements interval="60" />
    </ehcache:config>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation"  value="classpath:ehcache.xml"/>
    </bean>

    <!-- Aspect Oriented Performance Log Mekanizmasi -->
    <aop:aspectj-autoproxy />
    <bean id="performanceLogger" class="com.mkyong.util.PerformanceLogger" />

</beans>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

com.mkyong.jmx.JmxCoreComands
添加了以下配置:

<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" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"   
    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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd"
    default-lazy-init="false">


    <context:component-scan base-package="com.mkyong" />

    <context:property-placeholder location="config.properties" />

    <bean id="helloBean" class="com.mkyong.common.HelloWorld">
        <property name="name" value="Ahmet" />
    </bean>

    <bean id="person" class="com.mkyong.common.Person">
        <constructor-arg type="java.lang.String" value="DefaultName"/>
        <constructor-arg type="int" value="30"/>
    </bean>

    <bean id="jmxAdapter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="beans">
            <map>
                <entry key="SPRING:Name=TestRun">
                    <ref bean="jmxService" />
                </entry>
            </map>
        </property>
        <!-- managemethods property starts -->
        <property name="assembler">
            <bean
                class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
                <property name="managedInterfaces">
                    <value>com.mkyong.jmx.JmxCoreComands</value>
                </property>
            </bean>
        </property>
        <!-- managemethods property ends -->
    </bean>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>      
    </bean>

    <!-- Cache Mekanizmasi -->  
    <ehcache:annotation-driven />
    <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements interval="60" />
    </ehcache:config>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation"  value="classpath:ehcache.xml"/>
    </bean>

    <!-- Aspect Oriented Performance Log Mekanizmasi -->
    <aop:aspectj-autoproxy />
    <bean id="performanceLogger" class="com.mkyong.util.PerformanceLogger" />

</beans>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong.common</groupId>
  <artifactId>SpringExample</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringExample</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>com.googlecode.ehcache-spring-annotations</groupId>
      <artifactId>ehcache-spring-annotations</artifactId>
      <version>1.2.0</version>
    </dependency>



  </dependencies>
</project>

4.0.0
com.mkyong.common
春天的例子
罐子
1.0-快照
春天的例子
http://maven.apache.org
朱尼特
朱尼特
3.8.1
测试
org.springframework
弹簧芯
3.2.2.1发布
org.springframework
spring上下文
3.2.2.1发布
org.springframework
SpringJDBC
3.2.3.1发布
com.oracle
ojdbc14
10.2.0.3.0
org.springframework
春季方面
3.2.3.1发布
com.googlecode.ehcache-spring-annotations
ehcache-spring注释
1.2.0

看起来您没有配置TransactionManager,也没有拾取
@Transactional
的功能


尝试添加
和名为
transactionManager
JpaTransactionManager
bean,您需要在context.xml中启用Spring事务

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
transactions.xml

xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
">

您可以为此共享您的spring配置吗?听起来好像没有拾取
@Transactional
,添加了spring配置。添加了,但回滚没有成功执行。您使用的是spring jpa吗?好的,看起来您直接使用的是JdbcTemplate,尝试将JpaTransactionManager更改为DataSourceTransactionManager,如我的回答中所示。我已经添加了您在我的spring配置中键入的内容,但它不起作用。我的应用程序是J2SE应用程序。