-->
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.  [ 4 posts ] 
Author Message
 Post subject: Need to run set of sql code for each new session
PostPosted: Wed Sep 30, 2009 2:12 pm 
Newbie

Joined: Wed Sep 30, 2009 2:08 pm
Posts: 3
I have the need to run some sql after each new session. This must be run before any of the data access code that I actually care about. Any ideas on how to do this. The sql that needs to be run is:

Code:
mo_global.set_policy_context('S', 44);


Thanks.


Top
 Profile  
 
 Post subject: Re: Need to run set of sql code for each new session
PostPosted: Wed Sep 30, 2009 5:11 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
I guess I can think of two ways at least and hopefully serves your need..
1)Use a HibernateSessionFilter on the URL's you are interested in and in the filter execute/run the code you wish to.
2)Do you have a centralized place for getting Hibernate session? We use a Utility class which returns the Session Object.
The class definition looks something like this:-
public class HibernateSessionUtil implements HibernateSessionFactory
{
//utility methods to get the hibernate session using the thread local..
}

Hope this helps,
Srilatha.


Top
 Profile  
 
 Post subject: Re: Need to run set of sql code for each new session
PostPosted: Wed Sep 30, 2009 5:21 pm 
Newbie

Joined: Wed Sep 30, 2009 2:08 pm
Posts: 3
I have a Util class to get the session factory. Should I implement my own HibernateSessionFactory and return this in my factory. Then how do I actually execute the sql that I need?

Code:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class SessionFactoryUtil {

   private static SessionFactory sessionFactory;
   
   // disable the constructor
   private SessionFactoryUtil() {
   }
   
   static {
      // annotation and xml
      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
      // xml only
      //sessionFactory = new Configuration().configure().buildSessionFactory();
   }
   
   public static SessionFactory getInstance() {
      return sessionFactory;
   }
}


Top
 Profile  
 
 Post subject: Re: Need to run set of sql code for each new session
PostPosted: Thu Oct 01, 2009 11:51 am 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
Please see this post:-
viewtopic.php?f=1&t=959557&p=2305817&hilit=implements+HibernateSessionFactory#p2305817

Just in case the link does not work, here is the implementation of HibernateSessionFactory
HibernateSessionFactoryImpl :

Code:
public class HibernateSessionFactoryImpl implements HibernateSessionFactory {

static Log log = LogFactory.getLo(HibernateSessionFactoryImpl.class);

static final SessionFactory sessionFactory ;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal threadLocal = new ThreadLocal();

public Session getSession() {
Session s = (Session) threadLocal.get();
// Open a new Session, if this Thread has none yet
if (s == null) {

//Create the Hibernate session
s = sessionFactory.openSession();
//Set the Hibernate session in this Thread ;
threadLocal.set(s);
}
return s;
}


add other methods like closeSession(),beginTransaction, commitTransaction appropriately.

Hope this helps,
Srilatha.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.