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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Spring @Transactional with Raw Query  RSS feed

 
Greenhorn
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);
ps.executeUpdate();

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.

Thanks,
Eddy
 
Sheriff
Posts: 21601
101
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.
 
Bartender
Posts: 20307
110
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.
 
Bartender
Posts: 1002
16
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
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!