views:

132

answers:

1

Hi

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.

+2  A: 

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.

Espen
So I guess, the JdbcTemplate will simply commit after every update, if nothing else is specified?
Flyhard
That is default behaviour for every `DataSource` I have used. So it should, but check your `DataSource` configuration if the `JdbcTemplate.update(..)` doesn't commit.
Espen