Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbctemplate and jndi

 
Nilabhra Banerjee
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nilabhra Banerjee
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic