2015-03-20 5 views
5

. Im folgenden Code wirft Exception die Transaktion nicht zurück, sondern wirft eine RuntimeException.Das Auslösen einer RuntimeException führt dazu, dass die Transaktion zurückgesetzt wird, die Ausnahme jedoch nicht in einer Springstart-App

@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. :(

    } 
} 

Mein Repository:

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

Frühling Boot 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 

Irgendwelche Ideen, warum dies geschieht?

Antwort

9

Vom docs:

Alle Runtime löst Rollback, und jede geprüfte Ausnahme nicht.

können Sie dieses Verhalten außer Kraft setzen, indem rollbackFor oder rollbackForClassName auf der @Transactional Anmerkung angeben. Siehe oben Dokumente für eine ganze Reihe von Optionen.

Verwandte Themen