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.
You can configure each
javax.sql.DataSourceobject to enable auto-commit if that does the job, or disable auto-commit and write the transaction logic programatically.Both the
java.sql.Connectionand thejavax.sql.DataSourceclass 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.JdbcDaoSupportclass, then you have a JdbcTemplate object available for you with the derivedgetJdbcTemplate()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
@Transactionalannotation is impossible. You can however, inject the transaction manager into your service class. This is described in the reference documentation here.