2015-03-20 13 views
5

Aşağıdaki kodda Exception atma işlemi geri alma işlemi gerçekleştirmez, ancak RuntimeException atar.Bir RuntimeException atma işlemi geri alma işlemine neden olur, ancak özel durum bir bahar önyükleme uygulamasında yoktur.

@Service 
public class HelloService {  
    @Autowired 
    protected CustomerRepository repository; 
    @Transactional 
    public void run() throws Exception { 
     repository.save(new Customer("Jack", "Bauer")); 
     throw new RuntimeException("Kabooom!!!"); //Transaction is rolled back. Database is empty :) 
     //throw new Exception("Kabooom!!!"); //If this is used instead the records are inserted into the database. :(

    } 
} 

Benim depo:

public interface CustomerRepository extends CrudRepository<Customer, Long> { 
} 

Bahar önyükleme appliction.properties:

# DataSource settings: set here configurations for the database connection 
spring.datasource.url = jdbc:mysql://localhost/hobbadb 
spring.datasource.username = root 
spring.datasource.password = 
spring.datasource.driverClassName = com.mysql.jdbc.Driver  
# Specify the DBMS 
spring.jpa.database = MYSQL  
# Show or not log for each sql query 
spring.jpa.show-sql = true  
# Hibernate settings are prefixed with spring.jpa.hibernate.* 
spring.jpa.hibernate.ddl-auto = update 
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy 
spring.jpa.hibernate.hbm2ddl.auto= create-drop 

bunun neden olduğunu bir fikir? docs itibaren

cevap

9

:

Herhangi RuntimeException geri alma tetikler ve herhangi İstisna yok kontrol etti.

Sen @Transactional notu üzerine rollbackFor veya rollbackForClassName belirterek bu davranışı geçersiz kılabilirsiniz. Tam seçenekler için yukarıdaki belgelere bakın.

İlgili konular