-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Spring/Hibernate session question ... should be easy =)
PostPosted: Tue Nov 23, 2010 12:32 pm 
Newbie

Joined: Tue Nov 23, 2010 12:07 pm
Posts: 2
Hi all,

I'm newish to Spring/Hibernate (haven't used it in 2 years anyway) and I'm trying to change an existing app to work with an Oracle database that uses VPD (virual private database). Basically: the app serves several different customers and I wanted it to work like this:

1. Customer request comes in via web service.
2. Initial validation done on request, get customer id.
3. Set session context in db using customer id (this is done by calling a stored procedure our dba set up via org.springframework.jdbc.object.StoredProcedure).
4. Make DAO calls and do db stuff (the daos can only see the "current" customer).

Now, I have all this working ... kind of. When our QA got ahold of it they found that sometimes the request wouldn't find the customer data ... I assume this is due to the session context not being set for the session the daos are using.

I'm using dependency injection with a session factory that looks like this:
Code:
   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource">
         <ref bean="PayerHostingDataSource" />
      </property>
...


Sooooo, here's what the code looks like:

Code:
setSessionDao.setSession(id, "S");


which looks like:

Code:
public String setSession(String entityID, String idType)
    {
       CallSetSession proc = new CallSetSession(dataSource);
        proc.execute(entityID, idType);
       
        return "";
    }

public class CallSetSession extends StoredProcedure {

   private static final String STORED_PROC_NAME = "SP_NAME";

    public CallSetSession(DataSource ds) {
        super(ds, STORED_PROC_NAME);

        declareParameter(new SqlParameter("P_ENTITY_ID", Types.VARCHAR));
        declareParameter(new SqlParameter("P_ID_TYPE", Types.VARCHAR));
        compile();
    }

    public String execute(String entityID, String idType) {
        Map inParams = new HashMap(2);
        inParams.put("P_ENTITY_ID", entityID);
        inParams.put("P_ID_TYPE", idType);

        Map outParams = execute(inParams);

    }
}

So after I call setSession: I proceed to make dao calls from the same parent class (all injected with the same session factory) which access that customer's data ... usually =(.

EG:

Code:
customerDAO.getLocation(locationId);
customerDAO.getOtherStuff(stuff);


Here's what I'm using:

- Using hibernate 3.2.5
- C3p0 0.9.0.4
- Spring 2.5.2
- Spring-Hibernate 3.2.0.8

So questions:

1. Is there a way to log which session is being used by each component?
2. How can I get the session I use to make the setSession call to be used for all the other calls (thus scoping the data to only that customer).
3. What's the best/easiest way to achieve this with minimal code change?


Top
 Profile  
 
 Post subject: Re: Spring/Hibernate session question ... should be easy =)
PostPosted: Thu Dec 02, 2010 12:26 pm 
Newbie

Joined: Tue Nov 23, 2010 12:07 pm
Posts: 2
Just in case anyone cares =)

I got it figured out. My DBA didn't want to have to write the stored procedure that sets the session context such that it used a SYS_REFCURSOR (per the Hibernate documentation http://docs.jboss.org/hibernate/core/3.2/reference/en/html_single/#sp_query). So I figured I'd use the Spring StoredProcedure class. The problem was that since I was using Spring HibernateDaoSupport and getHibernateTemplate to implicitly control the session handling in the other DAOs (that actually access the data): when calling the Stored Procedure outside getHibernateTemplate I was not guaranteed to be using the same session after setting the context. So I finally convinced the DBA to rewrite the SP to use the SYS_REFCURSOR which allowed me to call it via:

Code:
getHibernateTemplate().findByNamedQuery("setSession", params);


Which then uses the same session for the entire set of DB reads ... and we're all good.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.