• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Spring JdbcTemplate - Setting auto commit

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am using Spring's JdbcTemplate class to work with JDBC. I am trying to run an insert operation in the database. As i understand, the jdbcTemplate.update() method will automatically commit the transaction and close the connection. I would be interested to find out if i can by some means set the auto commit to false.

I tried the following code, but it doesnt seem to work out.


This code still inserts data into the table.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is it you're trying to accomplish ?

If you're writing a test that accesses the database and you want to make sure the changes you write to the db don't actually get committed, try using Spring's AbstractTransactionalSpringContextTests. Your test can subclass this instead of TestCase and your test will run inside a transaction which will automatically rollback when the test method completes.
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi karthik,

You seem to need the current connection, you can get it with:
Connection conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());

See: http://forum.springframework.org/archive/index.php/t-9704.html, look at the post by Colin

Hope this helps.

Herman
PS: with Spring it's usually easier to handle transactions etc outside of your Java code.
 
Karthik Vijayarajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the inputs.
I was not trying to run any tests. Just experimenting with Spring's JDBC support.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No tested yet but think this can help

<bean id="autoCommittingJdbcTemplate" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="jdbcTemplate" />
<property name="proxyTargetClass" value="true" />
<property name="interceptorNames">
<list><value>autocommitInterceptor</value></list>
</property>
</bean>
 
Guru Agrawal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer

http://sujitpal.blogspot.com/2006/12/spring-jdbctemplate-with-autocommit.html

for more information
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic