Spring + + hibernate mysql causes, and things are not rolled back

    Recent projects suddenly something went wrong, and then found a way to use a class Service layer below where there's no rollback of the transaction. Then I wrote a test method after N tests are not rolled back. The following is part of the test method:

@Transactional(propagation =Propagation.REQUIRED,rollbackFor=RuntimeException.class)

public String getOnLineNum(String securecrt){
        History his=new History();
        his.setItem("123");       

        // Here a purposely provided that it exceeds the field length error
        Ludan Ludan new new LU = ();
        lu.setRoomName ( "33333333333333333");
        
        historyDao.save (His);
        ludanDao.save (LU);
              
        return "123";
    }

After the code above tests are many times will history record stored in the table, but not into ludan error, this is equivalent to the transaction was not rolled back.

The following is a part of the configuration of Spring:

    <!-- spring 事务管理 start-->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>

    <! - Statement Notes transaction ->  
    <tx: Transaction-Driven-Annotation Manager = "transactionManager" />
======================= ============== gorgeous dividing line =============================== =============

      One. As is the problem, then go through a wide range of inspection and search, and finally summed up the reason for several Spring transaction is not rolled back:

1. Capture yourself an exception, and not thrown to let Spring know.

2. No Spring in the transaction properly configured.

3.Service-seated problems that some logical problems.

4.MySQL does not support transactions.


two. Solution

The focus here explain solutions to a fourth reason on the line, this is the problem I have encountered in the project. MYSQL database that is the default mode is MyISAM, and this model does not support transactions, and at the time of the default install mysql database schema is MyISAM. In mysql, enter: show engines; you will see the following screen:

It can be seen only InnoDB support transactions. Therefore, to achieve the transaction must first convert mysql mysql database schema into InnoDB.

Specific methods are as follows:

1. Open the file in the root directory my.ini obtained, was added [mysqld] below:

default-storage-engine=INNODB

If there are skip-innodb, it can put off-line note.

2. Restart the mysql service

3. re-enter the mysql, enter: show engines; you can see the default database schema is already INNODB up;

4.-existing table later you will need to delete and re-build is a INNODB of, and you do not want to delete, then enter mysql, update it engine at the table

Execute: alter table table name ENGINE = InnoDB;

Then performing: show table status from the database name where name = 'table'; you can view the status of the

5. Test the above code again found the transaction is rolled back.

three. to sum up

    For this their problems could be the problem most people have experienced it, it can be considered due to their lack of experience, and therefore recorded convenient as I did go on the road procedure of friends, hoping to everyone there are some help, also do its own work record it, you can come back later to review again.

Published 66 original articles · won praise 5 · Views 100,000 +

Guess you like

Origin blog.csdn.net/u011622226/article/details/43985929