Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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: 21465
97
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: 19984
95
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.
 
Rancher
Posts: 981
9
Java Linux Spring
  • 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).  



 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!