Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Spring Beans Scope Usage

 
Ranch Hand
Posts: 431
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are generally defined set of classes - FE -> Controller > Service > DAO -> DB. Here, we autowire service in controller and dao in service layers. And, by default the scope of beans is singleton only.

Suppose 2 users come in and hit the controllers so won't these singleton beans of service or dao create an issue there? How is it handled in real?
 
Saloon Keeper
Posts: 22646
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends. If you use JPA for persistence, the transaction locking mechanisms come into play. Plus, you'll throw a ConcurrentModificationException if two processes attempt to modify the database at the same time.

Reading is OK, since reading is supposed to be idempotent. And as a bonus, an ORM can save overhead by reading the first query and pulling the second one from cache, if the system is set up that way.
 
Vaibhav Gargs
Ranch Hand
Posts: 431
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim, I am sorry but didn't understand it completely. Can you please explain little with an example.
 
Tim Holloway
Saloon Keeper
Posts: 22646
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in point of fact, I've never seen a complete list of the things that enforce transactions. Some are part of Spring, some are part of the database itself and some occur at differing levels and then you can also define your own tangled web of transaction types.

However, at its basic level, when two transactions commit with conflicts, the conflict is caught by comparing the entities field-by-field for equality (the "equals()" method is not good enough here, as it has a special meaning in JPA). If the fields don't match, that causes a ConcurrentModificationException to be thrown and the transaction fails and is rolled back. If your paranoia level is sufficiently high (and as I recall, it is by default), JPA will actually read the current record from the database and compare, not just look at local changes.

This probably won't detect concurrency violations on a single Entity instance (that is, where Entity instance A and Entity instance B are not only "equals()", but "=="), because that's an actual synchronization error, but if there are multiple transactions running against copies of the same("equals()") Entity that are not "==", that's how trouble is prevented.
 
reply
    Bookmark Topic Watch Topic
  • New Topic