java - how to achieve real transaction in spring with @Transactional -
i have 2 repositories - every repository have save methods different object. want execute these 2 methods transaction, means if 1 of them fail, transaction rolled back. (this meaning of transaction in opinion).
here code:
@repository public class bananarep { private jdbctemplate jdbc; @autowired public bananarep(jdbctemplate jdbc) { this.jdbc = jdbc; } public void savebanana(banana banana) { jdbc.update("insert ......", ...); } } @repository public class tomatorep { private jdbctemplate jdbc; @autowired public tomatorep(jdbctemplate jdbc) { this.jdbc = jdbc; } public void savetomato(tomato tomato) { jdbc.update("insert ... ", ....); } } @restcontroller public class mixcontroller { @autowired bananarep bananarep; @autowired tomatorep tomatorep; @transactional @requestmapping(value="/mix", method= requestmethod.get) public car mix() { banana banana = new banana(....); bananarep.savebanana(banana ); tomato tomato= new tomato(...); tomatorep.savetomato(tomato); return banana; } }
my build:
buildscript { ext { springbootversion = '2.0.0.m2' } repositories { mavencentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' version = '0.0.1-snapshot' sourcecompatibility = 1.8 repositories { mavencentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } } dependencies { compile('org.springframework.boot:spring-boot-starter-jersey') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-jdbc') compile('mysql:mysql-connector-java') }
in order test change name of 1 db column in tomato table, in controller savetomato(tomato) method fail. want if happen saved banana rolled back. thought should work @transactional method annotation wrong.
how achieve goal then?
transaction control should placed in service business processing layer
Comments
Post a Comment