Spring Boot - @Transactional - 不开放交易
SpringBoot - @Transactional - not opening transaction
我正在尝试在 SpringBoot 应用程序中使用事务处理,但由于某种原因它无法正常工作。
下面是示例应用程序的代码。我在 MyRestController.java 中休息 api 调用 DBService.hello() 方法。在 hello() 方法中,我使用 JOOQ 将一行插入到 table 中。我看到事务没有被创建,在进一步调试后我看到 DataSourceTransactionManager.doBegin() 方法没有被调用,这解释了为什么没有创建事务。
任何人都可以指出这个简单的代码有什么问题吗?
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)
public class JooqtransactionApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(JooqtransactionApplication.class, args);
}
}
MyRestController.java
@RestController
public class MyRestController {
@Autowired
DBService DBService;
@GetMapping("/callHello")
void invokeHello()
{
DBService.hello();
}
}
DBService.java
@Service
public class DBService {
@Autowired
DSLContext ctx;
@Transactional()
void hello() {
ctx.insertInto(Prospectiveclient.PROSPECTIVECLIENT).values("val1", "val2", "val3", "", LocalDateTime.now()).execute();
}
}
Bean 的创建
@Configuration
public class GlobalConfiguration {
@Autowired
private Environment environment;
@Bean
DataSource dataSource() {
HikariConfig config = new HikariConfig();
HikariDataSource ds;
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://localhost:3306/qadb");
config.setUsername("admin");
config.setPassword("pass");
return new HikariDataSource(config);
}
@Bean
public TransactionAwareDataSourceProxy transactionAwareDataSource() {
return new TransactionAwareDataSourceProxy(dataSource());
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSourceConnectionProvider connectionProvider() {
return new DataSourceConnectionProvider(transactionAwareDataSource());
}
@Bean
public ExceptionTranslator exceptionTransformer() {
return new ExceptionTranslator();
}
@Bean
public DefaultDSLContext dsl() {
return new DefaultDSLContext(configuration());
}
@Bean
public DefaultConfiguration configuration() {
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
jooqConfiguration.set(connectionProvider());
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
SQLDialect dialect = SQLDialect.valueOf("MYSQL");
jooqConfiguration.set(dialect);
return jooqConfiguration;
}
}
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation
only to methods with public visibility. If you do annotate protected,
private or package-visible methods with the @Transactional annotation,
no error is raised, but the annotated method does not exhibit the
configured transactional settings. Consider the use of AspectJ (see
below) if you need to annotate non-public methods.
我正在尝试在 SpringBoot 应用程序中使用事务处理,但由于某种原因它无法正常工作。 下面是示例应用程序的代码。我在 MyRestController.java 中休息 api 调用 DBService.hello() 方法。在 hello() 方法中,我使用 JOOQ 将一行插入到 table 中。我看到事务没有被创建,在进一步调试后我看到 DataSourceTransactionManager.doBegin() 方法没有被调用,这解释了为什么没有创建事务。 任何人都可以指出这个简单的代码有什么问题吗?
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)
public class JooqtransactionApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(JooqtransactionApplication.class, args);
}
}
MyRestController.java
@RestController
public class MyRestController {
@Autowired
DBService DBService;
@GetMapping("/callHello")
void invokeHello()
{
DBService.hello();
}
}
DBService.java
@Service
public class DBService {
@Autowired
DSLContext ctx;
@Transactional()
void hello() {
ctx.insertInto(Prospectiveclient.PROSPECTIVECLIENT).values("val1", "val2", "val3", "", LocalDateTime.now()).execute();
}
}
Bean 的创建
@Configuration
public class GlobalConfiguration {
@Autowired
private Environment environment;
@Bean
DataSource dataSource() {
HikariConfig config = new HikariConfig();
HikariDataSource ds;
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://localhost:3306/qadb");
config.setUsername("admin");
config.setPassword("pass");
return new HikariDataSource(config);
}
@Bean
public TransactionAwareDataSourceProxy transactionAwareDataSource() {
return new TransactionAwareDataSourceProxy(dataSource());
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSourceConnectionProvider connectionProvider() {
return new DataSourceConnectionProvider(transactionAwareDataSource());
}
@Bean
public ExceptionTranslator exceptionTransformer() {
return new ExceptionTranslator();
}
@Bean
public DefaultDSLContext dsl() {
return new DefaultDSLContext(configuration());
}
@Bean
public DefaultConfiguration configuration() {
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
jooqConfiguration.set(connectionProvider());
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
SQLDialect dialect = SQLDialect.valueOf("MYSQL");
jooqConfiguration.set(dialect);
return jooqConfiguration;
}
}
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.