I am using spring boot in my application.
@EnableAutoConfiguration
@SpringBootApplication
public class PushMysqlTestApplication implements CommandLineRunner {
private static Logger LOGGER = LoggerFactory.getLogger(RunQuery.class);
@Autowired
JdbcTemplate jdbcTemplate;
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(PushMysqlTestApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
startTransaction();
}
public void startTransaction() throws InterruptedException {
LOGGER.info("=========== Transaction started ===========");
jdbcTemplate.execute("start transaction");
jdbcTemplate.execute("select * from push_multithread where status = 1 for update");
Thread.sleep(5000);
jdbcTemplate.execute("update push_multithread set status = 30 where id=1");
Thread.sleep(10000);
jdbcTemplate.execute("commit");
LOGGER.info("========= Transaction ending ===========");
}
}
What the above code does is, it starts transaction, update a table and commit using JDBCTemplate
.
Is their any better way to achieve above transation in spring boot using anootations or JPA etc?
-
\$\begingroup\$ I use Hibernate, so not sure my ideas will help you. I use CrudRepository interface for database requests. After that I create Service annotated bean. This bean contains Transactional methods So if I want to call request I call a Transactional method and it should call Repository method. \$\endgroup\$P_M– P_M2017年08月16日 12:39:23 +00:00Commented Aug 16, 2017 at 12:39
-
\$\begingroup\$ could you please update your answer with code \$\endgroup\$Prakash P– Prakash P2017年08月16日 13:27:12 +00:00Commented Aug 16, 2017 at 13:27
-
\$\begingroup\$ No I cannot. This is because I would have to write an article about this, though I not feel like can teach here, sorry. Though you can find the solution samples with search engines. \$\endgroup\$P_M– P_M2017年08月16日 15:58:59 +00:00Commented Aug 16, 2017 at 15:58
2 Answers 2
Did you try to use google for this? You have multiple way to do this :P
For example: @Transactional
annotation on method.
Or using XML file to wrap methods with transaction.
Just look for this on google: https://spring.io/guides/gs/managing-transactions/
-
\$\begingroup\$ I think using @Transactional is better approach \$\endgroup\$Prakash P– Prakash P2017年08月16日 14:42:13 +00:00Commented Aug 16, 2017 at 14:42
-
\$\begingroup\$ based on your suggestion I am now using
@Transactional public void startTransaction() throws InterruptedException { LOGGER.info("=========== Transaction started ==========="); jdbcTemplate.execute("select * from push_multithread where status = 1 for update"); jdbcTemplate.execute("update push_multithread set status = 100 where id=1"); Thread.sleep(10000); LOGGER.info("========= Transaction ending ==========="); }
\$\endgroup\$Prakash P– Prakash P2017年08月16日 14:42:54 +00:00Commented Aug 16, 2017 at 14:42 -
\$\begingroup\$ could you please add these lines to your answer so that I can accept your answer, this will help readers in future, as they will get modified code \$\endgroup\$Prakash P– Prakash P2017年08月16日 14:43:49 +00:00Commented Aug 16, 2017 at 14:43
You can use Spring data CrudRepository which does a lot of things for you out of the box. Just create an interface and "map" it to the object you want to persist in the database, by writing the class and the type the ID field of your entity in between <>, like so:
@Repository
public interface MyRepository extends CrudRepository<MyOjbect, Long> {
MyObject someDBQuery(String param)
}
then in your service, autowired it and call your method, like so:
public class myService {
@Autowired
MyRepository myRepository;
@Transactional
public MyObject doSomething(String param) {
// call the repository
MyObject myObject = myRepository.someDBQuery(param);
}
}
Now before that, pleeease make sure you know what @Transactional is used for and WHY would you use it. More info here. Also see all the power CrudRepository has, because it has a lot to offer. More info - here
Before all this, import the spring-data-jpa library into your project. Good luck