在我的spring-boot应用程序中,我遇到了事务方法中的回滚问题。出于测试目的,我有这个例子:
@Transactional(rollbackFor = Exception.class)
public void insertPersonSaveLog() {
refService.insertPerson();
String a = null;
a.getBytes();
auditService.editRequest();
}
在日志中我可以看到应该执行回滚但是人仍然保存在我的数据库中。也许我错过了一些注释。这是主类:
@SpringBootApplication
@EnableScheduling
public class MyApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class).run(args);
}
}
和stacktrace:
149254 DEBUG org.springframework.transaction.jta.JtaTransactionManager - Initiating transaction commit
149254 DEBUG org.springframework.transaction.jta.JtaTransactionManager - Creating new transaction with name [..]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception
149254 DEBUG org.springframework.transaction.jta.JtaTransactionManager - Participating in existing transaction
149258 DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL query
149258 DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement
149258 DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
149258 DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Registering transaction synchronization for JDBC Connection
149260 DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement
149262 DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
149262 DEBUG org.springframework.transaction.jta.JtaTransactionManager - Initiating transaction rollback
java.lang.NullPointerException: null
at org.my.service.InserPersonService.insertPersonSaveLog(InserPersonService.java:77)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
DBCONFIG:
@Bean
@ConfigurationProperties(prefix = "datasource.ref")
@Primary
public DataSource refDataSource() {
return new DataSource();
}
@Bean
public JdbcTemplate refJdbcTemplate() {
return new JdbcTemplate(refDataSource());
}
@Bean
@ConfigurationProperties(prefix = "datasource.log")
public DataSource logDataSource() {
return new DataSource();
}
@Bean
public JdbcTemplate logJdbcTemplate() {
return new JdbcTemplate(logDataSource());
}
和application.properties:
datasource.ref.url=jdbc:oracle:thin:@something:1521:ora11g
datasource.ref.username=...
datasource.ref.password=...
datasource.ref.driver-class-name=oracle.jdbc.OracleDriver
datasource.log.url=jdbc:oracle:thin:@something:1521:ora11g
datasource.log.username=...
datasource.log.password=...
datasource.log.driver-class-name=oracle.jdbc.OracleDriver