In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to solve the abnormal error report of @ Transactional annotation". In the daily operation, I believe that many people have doubts about how to solve the problem of abnormal error report of @ Transactional annotation. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubt of "how to solve the abnormal error report of @ Transactional annotation". Next, please follow the editor to study!
Multiple sources of @ Transactional comments reporting errors
If you report an error after adding the @ Transactional annotation, check to see if the program is multi-source. There was a special chapter on the multi-source implementation of springboot. Adding annotations in the case of multiple data sources may cause problems. Here is the solution.
1. While configuring the data source
Be sure to annotate one of the configurations with @ Primary, and don't add the others.
Package com.wys.config;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary Import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;/** * @ program: * @ description: database configuration 1 * @ author: wys * @ create: 2019-12-03 16:20 * * / @ Configuration@MapperScan (basePackages = "com.wys.mapper.**", sqlSessionFactoryRef = "oneSqlSessionFactory") public class OneDataSourceConfig {@ Value ("${spring.datasource.one.driver-class-name}") String driverClass @ Value ("${spring.datasource.one.url}") String url; @ Value ("${spring.datasource.one.username}") String userName; @ Value ("${spring.datasource.one.password}") String passWord; @ Primary @ Bean (name = "oneDataSource") @ ConfigurationProperties ("spring.datasource.one") public DataSource masterDataSource () {DriverManagerDataSource dataSource = new DriverManagerDataSource (); dataSource.setDriverClassName (driverClass) DataSource.setUrl (url); dataSource.setUsername (userName); dataSource.setPassword (passWord); return dataSource;} @ Bean (name = "oneSqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("oneDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean (); sessionFactoryBean.setDataSource (dataSource) SessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:mybatis/mapper-postgre/*.xml")); return sessionFactoryBean.getObject ();} @ Bean (name = "oneSqlSessionFactory") public SqlSessionTemplate sqlSessionFactoryTemplate (@ Qualifier ("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate (sqlSessionFactory);}} 2. Be sure to be in the data source configuration that needs to use transaction annotations
Plus create a transaction manager
Package com.wys.config;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary Import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;/** * @ program: * @ description: database configuration 1 * @ author: wys * @ create: 2019-12-03 16:20 * * / @ Configuration@MapperScan (basePackages = "com.wys.mapper.**", sqlSessionFactoryRef = "oneSqlSessionFactory") public class OneDataSourceConfig {@ Value ("${spring.datasource.one.driver-class-name}") String driverClass @ Value ("${spring.datasource.one.url}") String url; @ Value ("${spring.datasource.one.username}") String userName; @ Value ("${spring.datasource.one.password}") String passWord; @ Primary @ Bean (name = "oneDataSource") @ ConfigurationProperties ("spring.datasource.one") public DataSource masterDataSource () {DriverManagerDataSource dataSource = new DriverManagerDataSource (); dataSource.setDriverClassName (driverClass) DataSource.setUrl (url); dataSource.setUsername (userName); dataSource.setPassword (passWord); return dataSource;} @ Bean (name = "oneSqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("oneDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean (); sessionFactoryBean.setDataSource (dataSource) SessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver () .getResources ("classpath:mybatis/mapper-postgre/*.xml")); return sessionFactoryBean.getObject ();} / / create transaction manager 1 @ Bean (name = "oneManager1") public PlatformTransactionManager txManager (@ Qualifier ("oneDataSource") DataSource dataSource) {return new DataSourceTransactionManager (dataSource) } / @ Bean (name = "oneSqlSessionFactory") public SqlSessionTemplate sqlSessionFactoryTemplate (@ Qualifier ("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate (sqlSessionFactory);}}
Just add the transactionManager configuration where comments are needed.
@ Transactional (transactionManager = "oneManager1", rollbackFor=Exception.class) @ Transactional several scenarios that @ RestControllerpublic class AController {@ Autowired AService aService; / / rollback @ GetMapping ("direct") public void direct () {aService.testTransactional ();} / / do not roll back @ GetMapping ("indirect") public void indirect () {aService.testTransactionalIndirect () } / / do not roll back @ GetMapping ("nonPublic") public void nonPublic () {aService.testTransactionalNonPublic ();} / do not roll back @ GetMapping ("catchException") public void catchException () {aService.testTransactionalCatchException ();} / / do not roll back @ GetMapping ("sqlException") public void sqlException () throws SQLException {aService.testTransactionalSQLException () @ GetMapping ("sqlExceptionWithRollbackfor") public void sqlExceptionWithRollbackfor () throws SQLException {aService.testTransactionalSQLExceptionWithRollbackfor ();}} @ Servicepublic class AService {@ Autowired TestTableDAO testTableDAO; / / rollback @ Transactional public void testTransactional () {ATestTable er = new ATestTable (); er.setSummary ("test"); testTableDAO.save (er); throw new RuntimeException ("exception") } / / No rollback: calling other methods within the class does not cause transactional behavior, even if the called method uses @ Transactional annotation to modify public void testTransactionalIndirect () {testTransactional ();} / / No rollback: @ Transaction annotation takes effect only for methods named pubic @ Transactional void testTransactionalNonPublic () {ATestTable er = new ATestTable () Er.setSummary ("test"); testTableDAO.save (er); throw new RuntimeException ("exception");} / / do not roll back @ Transactional public void testTransactionalCatchException () {ATestTable er = new ATestTable (); er.setSummary ("test"); testTableDAO.save (er); try {throw new RuntimeException ("exception") } catch (Exception e) {System.out.println ("catch");}} / / No rollback: @ Transactional rollback only RuntimeException and Error @ Transactional public void testTransactionalSQLException () throws SQLException {ATestTable er = new ATestTable (); er.setSummary ("test"); testTableDAO.save (er); throw new SQLException ("exception") by default } / / rollback: specify to roll back @ Transactional (rollbackFor = {SQLException.class}) public void testTransactionalSQLExceptionWithRollbackfor () throws SQLException {ATestTable er = new ATestTable (); er.setSummary ("test"); testTableDAO.save (er); throw new SQLException ("exception") when the SQLException exception occurs } @ Repositorypublic interface TestTableDAO extends JpaRepository, JpaSpecificationExecutor {} @ Entity@Data@Table (name = "test") public class ATestTable {@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) int id; @ Column (name = "summary", length = 512) String summary;} this is the end of the study on "how to solve the abnormal error report of @ Transactional annotations". I hope it can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.