my dog learned polymorphism*
The moose likes Spring and the fly likes jdbctemplate and jndi Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "jdbctemplate and jndi" Watch "jdbctemplate and jndi" New topic
Author

jdbctemplate and jndi

Nilabhra Banerjee
Ranch Hand

Joined: Mar 17, 2004
Posts: 34
I am new to swing. I have a basic question. My bean definition xml is as below. I am using a jndi connection pool.
Is jdbctemplate by default, closing my connection ? Or I have to explicitly call jdbcTemplate.getDataSource().getConnection().close() every time I use jdbTemplate?


Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

JDBC template will take care of closing your connections automatically. If the connection is determined to be bound to the thread by Springs Transactional facilities it will be released by its internal connectionHandler mechanism but not closed. This allows the connection to be made available to other operations sharing the same transaction.

In short no you should not need to close connections.


[How To Ask Questions][Read before you PM me]
Nilabhra Banerjee
Ranch Hand

Joined: Mar 17, 2004
Posts: 34
Thanks Bill for your help. I appreciate it greatly.

Continuing to think... loudly

Therefore, to get similar effect of commit and rollback of java:comp/UserTransaction, in Spring I can use PlatformTransactionManager.commit(TransactionStatus) or rollback(Tstatus) on top of JdbcTemplate. JDBCTemplate will release the connection from PlatformTransaction and close it (or reuse it ? Can it reuse it ?).

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

You can use the PlatformTransactionManager, however transactions are a cross cutting concern which makes them a good candidate for AOP. Spring has a very elegant solution for this with their @Transactional annotation. I would make use of it, with a few lines of XML or a an annotation(if you are using java config) you can configure Annotation based transactional support, and then simply annotate your methods with @Transactional. This will automatically start the transaction when you enter and commit the transaction rolling back if any runtime exception is thrown out when you exit. It can be configured to roll back for checked exceptions as well.

Take a look around the forums here or in the Spring reference documentations for some examples.

http://static.springsource.org/spring/docs/3.2.1.RELEASE/spring-framework-reference/htmlsingle/#transaction-declarative-annotations
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jdbctemplate and jndi
 
Similar Threads
Spring + JDBC + Servlet
No suitable driver Error in Test Class
Using SPRING hibernate and JDBC templates in the same DAO
Connecting to two schemas using hibernate
DataSource null in DWR