2
\$\begingroup\$

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?

\$\endgroup\$
3
  • \$\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
    Commented Aug 16, 2017 at 12:39
  • \$\begingroup\$ could you please update your answer with code \$\endgroup\$
    – Prakash P
    Commented 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
    Commented Aug 16, 2017 at 15:58

2 Answers 2

3
\$\begingroup\$

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/

\$\endgroup\$
3
  • \$\begingroup\$ I think using @Transactional is better approach \$\endgroup\$
    – Prakash P
    Commented 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
    Commented 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
    Commented Aug 16, 2017 at 14:43
3
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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