Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionManager and datasource in spring boot - spring data

I wanted to access the Transaction Manager and datasource in my configuration in spring boot application. I am using spring-boot-starter-data-jpa artifact.

Is it possible to just autowire in the config and get its access?

like image 218
krmanish007 Avatar asked Dec 24 '22 06:12

krmanish007


1 Answers

You can get access to the transaction manager with:

@Autowired
private PlatformTransactionManager transactionManager;

For the DataSource, out-of-the-box with the starter you chose you will get the tomcat-jdbc datasource:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-configure-datasource

You can just inject that like this:

@Autowired
private DataSource dataSource;

Make sure you use the JDBC DataSource type (javax.sql.DataSource), and not a specific implementation.

like image 195
leeor Avatar answered Feb 04 '23 12:02

leeor