Spring 3 Annotations: declarative transaction management without using xml-less

I am creating a spring 3 explanatory entry (xml-less) application after the tutorial here

And here is my configuration file

@Bean(name = "dataSource") public DriverManagerDataSource dataSource() { DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); driverManagerDataSource.setDriverClassName("org.postgresql.Driver"); driverManagerDataSource.setUrl("jdbc:postgresql://localhost:5432/test"); driverManagerDataSource.setUsername("postgres"); driverManagerDataSource.setPassword("gayle"); return driverManagerDataSource; } @Bean(name = "studentJDBCTemplate") public StudentJDBCTemplate studentJDBCTemplate() { StudentJDBCTemplate studentJDBCTemplate = new StudentJDBCTemplate(); studentJDBCTemplate.setDataSource(dataSource()); studentJDBCTemplate.setDataSourceTransactionManager(dataSourceTransactionManager()); studentJDBCTemplate.setJdbcTemplate(new JdbcTemplate()); return studentJDBCTemplate; } @Bean public DataSourceTransactionManager dataSourceTransactionManager() { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource()); return dataSourceTransactionManager; } 

Now I am trying to do declarative transaction management , therefore my create() method inside StudentJDBCTemplate

 public void create(String name, Integer age) { System.out.println("Creating!"); String SQL = "insert into Student (name, age) values (?, ?)"; jdbcTemplate.update(SQL, name, age); System.out.println("Created Record Name=" + name + " Age=" + age); } 

Doesn't make a transaction manager soft call.

How do I achieve this? Should I declare @Aspect for this? Is there any annotation that can automatically configure when to make transactions?

+4
source share
1 answer

You want to use the @Transactional annotation. Here is the documentation for him. In particular, go to section 10.5.1. All you have to do is annotate the methods with which you want to conclude a transaction with annotation. Using annotation values, you can specify anything you want about the type of transaction.

+2
source

Source: https://habr.com/ru/post/1489848/


All Articles