Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Spring @Transactional with Raw Query  RSS feed

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

newbie here.

lets say if I use direct sql query, without JPA.

Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);

Something like above(incomplete). On the service method, if I put annotation @Transactional, does it have any benefit to it?
since when I don't use JPA/Hibernate I don't do begin transaction etc.

Posts: 21601
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connections also have transactions. In "normal" JSE mode, these are automatically committed unless you call setAutoCommit(false). In Spring and JEE these are managed by Spring or your container.
Posts: 20307
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a matter of whether you're using an ORM. The @Transactional annotation invokes Spring functionality. You should be able to use it with raw JDBC (I think), but only if you're doing so via the Spring JDBC Data Manager.

Although truthfully, once you get something complex enough to require Spring, an ORM is a good investment. Hibernate can manage all but the lightest database interactions much more efficiently than you can using raw JDBC.
Posts: 1002
Java Linux Mac OS X Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring offers a template JDBC Template which aims to simplify JDBC code and helps with all of those horrid checked exceptions.  The template pattern is used quite a lot throughout the spring framework, so its worth knowing...   However as stated above - you may just want to use JPA or even Spring Data.

The Spring Docs give a good overview of Transaction management which could be used with JDBC or JPA if the underling resource supports it (typically a database).  

Villains always have antidotes. They're funny that way. Here's an antidote disquised as a tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!