Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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 跨国家韩元';t在休眠时回滚_Java_Spring_Hibernate - Fatal编程技术网

Java 跨国家韩元';t在休眠时回滚

Java 跨国家韩元';t在休眠时回滚,java,spring,hibernate,Java,Spring,Hibernate,我正在尝试将回滚机制添加到我的spring项目中,在遵循了一些教程之后,我仍然无法解决为什么在以下代码中不会发生回滚的问题: @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = TransactionException.class) public boolean changeSquad(ArrayList<Integer> playerIdList, String ema

我正在尝试将回滚机制添加到我的spring项目中,在遵循了一些教程之后,我仍然无法解决为什么在以下代码中不会发生回滚的问题:

@Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = TransactionException.class)
    public boolean changeSquad(ArrayList<Integer> playerIdList, String email) {
        boolean squadChanged = false;

        // Obtain the current list of players from the persistent storage
        TypedQuery<Player> playersQuery = emgr.createNamedQuery("Player.getUsersPlayers", Player.class);
        playersQuery.setParameter("email", email);



        List<Player> currentPlayerList = playersQuery.getResultList();

        // Make a copy of the list obtained as method parameter
        ArrayList<Integer> playerIdListCopy = new ArrayList<Integer>(playerIdList);

        // Make a copy of the list obtained from persistent storage
        ArrayList<Player> currentPlayerListCopy = new ArrayList<Player>(currentPlayerList);
        System.out.println("current player list copy = " + currentPlayerListCopy);



        // Iterate through each player from the currentPlayerList
        for (int i = 0; i < currentPlayerListCopy.size(); i++) {
            // On each iteration check if the playerIdList contains currently iterated 
            // Player's id, if so then eliminate it from both lists
            int playerId = currentPlayerListCopy.get(i).getPlayer_id();

            if (playerIdListCopy.contains(playerId)) {
                currentPlayerListCopy.remove(i);
                playerIdListCopy.remove(playerIdListCopy.indexOf(playerId));
                i--;
            }

        }
        // IF copy of current player list AND playerId list is empty, proceed
        if (currentPlayerListCopy.isEmpty() && playerIdListCopy.isEmpty()) {
            System.out.println("currentPlayerListCopy && playerIdListCopy = empty");

            // Make the squad changes

            for (int j = 0; j < currentPlayerList.size(); j++) {
                Player tempPlayer = currentPlayerList.get(j);
                int tempPlayerId = tempPlayer.getPlayer_id();

                // LOOP for all players from the client side list
                for (int n = 0; n < playerIdList.size(); n++) {
                    if (playerIdList.get(n) == tempPlayerId) {
                        ......
                    } 
                } // END loop for all players from the client side list 

                emgr.persist(tempPlayer);
                if (j == 13) {
                    System.out.println("j13, throwing exception");
                    try {
                        throw new TransactionException("sh.t happens");
                    } catch (Exception e) {
                        System.out.println("thrown and catched");

                        e.printStackTrace();
                    }
                }
            } // END loop for the player from the server side list
        } else {
            System.out.println("Seems like somebody messed up with the values.");
        }
        return squadChanged;
    }
@覆盖
@事务性(传播=propagation.REQUIRED,rollboor=TransactionException.class)
公共布尔更改组(ArrayList playerIdList,字符串电子邮件){
布尔squadChanged=false;
//从持久存储中获取当前玩家列表
TypedQuery playersQuery=emgr.createNamedQuery(“Player.getUsersPlayers”,Player.class);
playersQuery.setParameter(“email”,email);
List currentPlayerList=PlayerQuery.getResultList();
//复制作为方法参数获得的列表
ArrayList playerIdListCopy=新的ArrayList(playerIdList);
//制作一份从持久存储中获取的列表的副本
ArrayList currentPlayerListCopy=新的ArrayList(currentPlayerList);
System.out.println(“当前玩家列表副本=“+currentPlayerListCopy”);
//从currentPlayerList遍历每个玩家
对于(int i=0;i
在抛出TransactionException时,我希望回滚机制能够正常工作,但是它不能正常工作,并且实体被持久化

下面是persistence.xml

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">

    <!-- CREATES DATA SOURCE -->
    <!-- <bean class="java.net.URI" id="dbUrl">
        <constructor-arg value="#{systemEnvironment['DATABASE_URL']}" />
    </bean> -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url"
            value="#{ 'jdbc:postgresql://' + 'localhost' + ':' + '5432' + '/handball' }" />
        <property name="username" value="'#{ 'postgres' }" />
        <property name="password" value="#{ '........' }" />
    </bean>

    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="emf">
        <!-- Entities location -->
        <property name="packagesToScan" value="af.handball.entity"></property>
        <property name="dataSource" ref="dataSource" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
            </props>
        </property>

         <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
        </property>  

    </bean>
</beans>

假的
更新
org.hibernate.dialogue.PostgreSqlDialogue
假的

如果您能告诉我哪里出了问题,我将不胜感激,谢谢

不要捕获事务方法中的异常。事务功能由封装服务对象的代理实现。异常必须到达代理才能让代理知道它需要回滚。如果在服务方法中捕获异常,那么代理不知道事务应该失败


删除异常周围的try-catch块并抛出异常,您应该会看到事务回滚。

TransactionManager的配置在哪里?:)