|
From: Colin S. <col...@ex...> - 2003-08-25 21:14:28
|
W/regards to auto commit in the default case (when the class gets the connection itself), I would say, exactly, the JDBC specs specifically say the getConnection called on the datasource will return a connection with autoCommit on by default. So why is SingleConnectionDataSource then going and setting it on as well. Given that SingleConnectionDatasource doesn't have any clue about the intended use of the connection, the most correct behaviour is probably to not have it call setAutoCommit with either state, by default, which would mirror normal datasource/connection usage, and also allow a mechanism to specify that it should be called one way or another. However, this may be more work than is worth it, and a default call with the option to on, along with a mechanism (another constructor with a value for the call) to override it, would be fine. So to be clear, I do think even when SingleConnectionDataSource gets the connection there is a need for an override. As for my use case, I don't think I was clear enough. I have some code in a Hibernate specific DAO/Mapper object. I am already executing inside a transaction introduced via Spring's AOP interceptor. So I do have a SessionFactory, etc. bound to the thread. Now inside a method in this mapper I need to do an operation on a BLOB/CLOB, where there are a number of db platform specific issues, and Hibernate's support of BLOBs/CLOBs is not good enough. Given that I have a Hibernate Session which already has a connection available, using it is the best mechanism (as opposed to trying to get a real DataSource in Spring's context to use). Now I am using JDBCTemplate and JDBCHelper as convenience classes, to wrap JDBC exceptions, and for the methods they provide. Now I could call DataSourceUtils.getThreadObjectManager().bindThreadObject(...) to bind the SingleConnectionDatasource to the thread, before using the JDBCTemplate, but I'm not sure that's buying me much over the case of not binding it. In either case, JDBCTemplate is simply going to end up getting the same Connection. Because I am using the SingleConnectionDatasource, it's not like in this case there is a real DataSource object, and you only want to call getConnection on it once... Hopefully my exaplanation is clear now... jürgen höller [werk3AT] wrote: >I agree that in case of an existing connection, SingleConnectionDataSource should not override the commit mode. For the default case, I consider auto commit mode appropriate though. Any J2EE DataSource will return an auto commit connection outside a transaction, so why shouldn't SingleConnectionDataSource do too? > >BTW, what you are trying to do seems a bit awkward. Why manually feed the Connection of a Hibernate Session via SingleConnectionDataSource to JdbcTemplate? JdbcTemplate is supposed to be a threadsafe, reusable object, fetching connections on demand. Wouldn't it be easier to use Spring's thread-binding mechanism (DataSourceUtils.getThreadObjectManager) to make JdbcTemplate use a specific connection, be it implicitly via HibernateTransactionManager or explicitly in some custom way? > >Juergen > > > > -----Ursprüngliche Nachricht----- > Von: Colin Sampaleanu [mailto:col...@ex...] > Gesendet: Mo 25.08.2003 20:02 > An: spr...@li... > Cc: > Betreff: [Springframework-developer] Why does SingleConnectionDataSource always set autoCommit=true? > > > > I want to use SingleConnectionDataSource with Hibernate, so I can wrap a > connection I get from a Hibernate Session, and feed it as a DataSource > to JDBCTemplate/JDBCHelper. > > However, right now I've had to make my own variant since the current > code always calls > source.setAutoCommit(true); > in the init() method. This seems very arbitrary to me. At least in the > case when the class is given an existing connection, it should just > assume the commit option has been set appropriately on the connection. > > Even in the case when the class is opening the connection itself from an > existing DataSource, there needs to be an option to set autoCommit > either way. > > Regards, > Colin > > |