Results 1 to 4 of 4

Thread: trying to wrap my head around spring.

  1. #1
    Join Date
    Dec 2004
    Posts
    19

    Default trying to wrap my head around spring.

    I'm sure you must get a million of these posts a week. I am still trying to wrap my head around spring after reading J2ee without ejb.

    Question:

    Let's say I have a class called search handler. It conducts some search, collects the results, does some filtering and returns a result set.

    With spring, I would get an instance of the search handler from the beanfactory. I am assuming that it would be a singleton since the class would not have to keep any state over function calls.

    However, how does this work from a threading point of view. ie. Lets say the method takes 10 seconds on average to complete, and there are 5 calls to it in a matter of milliseconds. Does it perform all these at once? Only if the method was synchronized would it have to wait, correct? I've always found it very confusing that one object could handle, ie 1000 requests per second.

    How does this all work?

    Thank you greatly!

    Jay

  2. #2

    Default

    The singleton does need to be thread-safe. However, that doesn't necessarily mean that its methods must be synchronized. If the object is stateless, there's no contention for shared data, so an arbitrary number of threads can execute inside it at once.

    This doesn't have anything to do with Spring, really. It's just how multithreaded programming works.

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    To just expand on the earlier answer, you'd pass your search criteria into your singleton method. eg:

    Code:
    private SearchDao search;
    
    public List search(String name) {
      return dao.search(name);
    }
    As the method does not modify any object-level properties, there is no need to worry about threading issues. Your DAOs are almost always the same, and just delegate to a RDBMS which takes care of synchronisation.

    On the other hand, this method would be a problem:

    Code:
    private Map bankAccounts = new MashMap();
    public void deposit(Long accountNumber, float amount) {
      ((Account)bankAccounts.get(accountNumber)).deposit(amount);
    }
    Hope that clears it up for you.

  4. #4
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    The caller would probably be configured by Dependency Injection. Thus it would hold a reference, rather than need to call getBean() on the factory (although of course that would work as well).

    As Ben points out, a single object will be the best solution for a stateless service.

    Spring also supports pooling a la SLSBs, for more complex threading scenarios, but they're actually pretty rare for this kind of service. Unlike EJB, Spring can provide pooling for any POJO.

    But as Ben also pointed out, the question is not really Spring-specific.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Replies: 5
    Last Post: Aug 9th, 2008, 05:30 AM
  3. A Spring Class Loader?
    By azzoti in forum Architecture
    Replies: 8
    Last Post: May 7th, 2005, 04:02 AM
  4. No one hires for Spring expertise
    By ctassoni in forum Meta
    Replies: 13
    Last Post: May 5th, 2005, 04:29 PM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •