Guest User

Untitled

a guest
Jan 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import org.springframework.boot.jdbc.DataSourceBuilder;
  2. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  6. import org.springframework.transaction.PlatformTransactionManager;
  7. import org.springframework.transaction.annotation.EnableTransactionManagement;
  8. import org.springframework.transaction.annotation.Transactional;
  9.  
  10. import javax.sql.DataSource;
  11.  
  12. @Configuration
  13. @EnableTransactionManagement
  14. public class TestInnerTxThrowingException {
  15.  
  16. public static void main(String[] args) {
  17.  
  18. AnnotationConfigApplicationContext applicationContext =
  19. new AnnotationConfigApplicationContext(TestInnerTxThrowingException.class);
  20.  
  21. final FooService fooService = applicationContext.getBean(FooService.class);
  22. fooService.foo();
  23. }
  24.  
  25. public class FooService {
  26.  
  27. private final BarService barService;
  28.  
  29. public FooService(BarService barService) {
  30. this.barService = barService;
  31. }
  32.  
  33. @Transactional
  34. public void foo() {
  35. try {
  36. barService.bar();
  37. } catch (Exception ex) {
  38. System.out.println("exception " + ex.getMessage());
  39. }
  40. }
  41. }
  42.  
  43. public class BarService {
  44.  
  45. @Transactional
  46. public void bar() {
  47. throw new RuntimeException("to rollback inner tnx");
  48. }
  49. }
  50.  
  51. @Bean
  52. PlatformTransactionManager transactionManager() {
  53. return new DataSourceTransactionManager(dataSource());
  54. }
  55.  
  56. @Bean
  57. DataSource dataSource() {
  58. return DataSourceBuilder
  59. .create().url("jdbc:h2:mem")
  60. .username("sa")
  61. .password("")
  62. .build();
  63. }
  64.  
  65. @Bean
  66. BarService barService() {
  67. return new BarService();
  68. }
  69.  
  70. @Bean
  71. FooService fooService() {
  72. return new FooService(barService());
  73. }
  74. }
Add Comment
Please, Sign In to add comment