Back Forum Reply New

Question about SingleConnectionDataSource

Hi,

I have a small application that uses straight JDBC to access ORACLE using quot;thinquot; driver. I create quot;connectionquot; and quot;statementquot; at the beginning and use this quot;statementquot; for all SQLs. I close them at the end explicitly.

I ran into spring the other day and am contemplating switching to using JdbcTemplate. I still want to use one connection created at the beginning and hence am thinking of using SingleConnectionDataSource instantiated with quot;connectionquot;. However, I am getting little bit confused about the other parameter quot;suppressClosequot;. Am I right in assuming that I need to pass quot;falsequot; for this parameter? I am planning to close the connection explicitly at the end as mentioned above.

Thanks in advance,
Raghu.

You are correct.  suppressClose should be true.  JdbcTemplate uses proper connection creation/closing semantics and will try to close the connection after each operation.  So you want to avoid that so you can do it yourself.

Hi,

Thanks for the reply. My confusion came from the fact that using SingleConnectionDataSource should have been sufficient to indicate that connection should not be closed after each operation and the user of jdbctemplate will close at the end. What is the need for this additional field quot;suppressClosequot;? The API doc in this regard was not clear to me and I am hoping that someone would elaborate about this particular parameter.

Thanks,
Raghu.

I beleive it is required in case other non Spring code calls close on the connection. Spring like you said knows not to close it.

If you set suppressClose = TRUE the api suggests to me that a proxy object wrapping the connection is passed back and the close method is suppressed.

I might be wrong........

That's correct.  If you only use Spring JDBC code, then you don't need to set quot;suppressClosequot;.  If non-Spring JDBC code is involved, then set this it to true just in case somewhere there is a close call in order to retrun connection to the pool.


Originally Posted by draghuramHi,

Thanks for the reply. My confusion came from the fact that using SingleConnectionDataSource should have been sufficient to indicate that connection should not be closed after each operation and the user of jdbctemplate will close at the end. What is the need for this additional field quot;suppressClosequot;? The API doc in this regard was not clear to me and I am hoping that someone would elaborate about this particular parameter.

Thanks,
Raghu.

Setting suppressClose to true is useful for unit testing. Combined with AbstractTransactionalSpringContextTests (default behaviour of this class is that transactions are not committed), it will prevent the connection from being closed. Thus, your DAO unit tests will reuse the same connection.

Dino
¥
Back Forum Reply New