Question

We are looking into using the JdbcTemplate for accessing the DB - but we have many different DB-connections, that each class could use, so injecting the jdbcTemplate is not an option atm. So if we do a

jdbcTemplate = new JdbcTemplate(dataSource);

what will the transaction policy be? Auto-commit is off in the DB.

Was it helpful?

Solution

You can configure each javax.sql.DataSource object to enable auto-commit if that does the job, or disable auto-commit and write the transaction logic programatically.

Both the java.sql.Connection and the javax.sql.DataSource class have methods for enable/disable auto-commit.

Regarding dependency injection and Spring, you can still inject a datasource object into your repository. If you also let each repository extend the org.springframework.jdbc.core.support.JdbcDaoSupport class, then you have a JdbcTemplate object available for you with the derived getJdbcTemplate() method.

You can also let Spring handle the transaction handling for you. Without a XA transaction manager, you need one transaction manager for each datasource. With many transaction managers, declarative transaction support with the @Transactional annotation is impossible. You can however, inject the transaction manager into your service class. This is described in the reference documentation here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top