I am using spring boot in my application.

public class PushMysqlTestApplication implements CommandLineRunner {
    private static Logger LOGGER = LoggerFactory.getLogger(RunQuery.class);
    JdbcTemplate jdbcTemplate;

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(PushMysqlTestApplication.class, args);

    public void run(String... args) throws Exception {

    public void startTransaction() throws InterruptedException {
        LOGGER.info("=========== Transaction started ===========");
        jdbcTemplate.execute("start transaction");
        jdbcTemplate.execute("select * from push_multithread where status = 1 for update");
        jdbcTemplate.execute("update push_multithread set status = 30 where id=1");
        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\$ – Pavlo Morozov Aug 16 '17 at 12:39
  • \$\begingroup\$ could you please update your answer with code \$\endgroup\$ – Prakash Pandey Aug 16 '17 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\$ – Pavlo Morozov Aug 16 '17 at 15:58

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/

| improve this answer | |
  • \$\begingroup\$ I think using @Transactional is better approach \$\endgroup\$ – Prakash Pandey Aug 16 '17 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 Pandey Aug 16 '17 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 Pandey Aug 16 '17 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:

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 {

MyRepository myRepository;

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.