• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 21719
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.
Saloon Keeper
Posts: 20658
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • 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: 1012
IntelliJ IDE Java Linux Mac OS X Oracle Redhat Spring Tomcat Server 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).  

No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!