Here's how I do it - in my TestCases, I call:

ses = ServiceLocator.currentSession();

And in my app, when running on Tomcat, I have a filter with the following
method:  

        public static Session getSession() throws HibernateException,
SQLException {
                return ServiceLocator.currentSession();
        }

And here's the contents of ServiceLocator - modeled after HibernateSession:

public class ServiceLocator {
    //~ Static fields/initializers
=============================================

    public static final ThreadLocal session = new ThreadLocal();
        private static SessionFactory sf = null;
        private static ServiceLocator me;
    private static Log log = LogFactory.getLog(ServiceLocator.class);

        static {
                try {
                        me = new ServiceLocator();
                } catch (Exception e) {
                        log.fatal("Error occurred initializing
ServiceLocator");
                        e.printStackTrace();
                }
        }
        
    //~ Methods
================================================================
        private ServiceLocator() throws HibernateException, SQLException {

                // Try to lookup a JNDI Connection
                try {
                        if (log.isDebugEnabled()) {
                                log.debug("Looking up Session in JNDI");
                        }
                        sf = (SessionFactory) new
InitialContext().lookup(Constants.SESSION_FACTORY);
                } catch (NamingException ne) {
                        if (log.isDebugEnabled()) {
                                log.info("error communicating with JNDI,
assuming testcase");
                        }

                        Datastore datastore = Hibernate.createDatastore();

        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.ChangeRequest
.class);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.ChangeRequest
Detail.class);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.CMCFChannelMa
p.class);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.CMCFIrt.class
);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.CMCFMaster.cl
ass);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.CMCFMps.class
);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.Headend.class
);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.HSCRFMaster.c
lass);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.HSCRFSignal.c
lass);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.MSO.class);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.Role.class);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.User.class);
        
datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence.ValidationRes
ults.class);

                        sf = datastore.buildSessionFactory();
                }
        }
        
    public static Session currentSession()
      throws HibernateException, SQLException {
        Session s = (Session) session.get();
                
        if (s == null) {

            s = sf.openSession();

            if (log.isDebugEnabled()) {
                log.debug("Opened hibernate session.");
            }

            session.set(s);
        }

        return s;
    }

    public static void closeSession() throws HibernateException,
SQLException {
        Session s = (Session) session.get();
        session.set(null);

        if (s != null) {
            s.close();

            if (log.isDebugEnabled()) {
                log.debug("Closed hibernate session.");
            }
        }
    }

> -----Original Message-----
> From: Joseph Fifield [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, February 05, 2003 3:19 PM
> To: [EMAIL PROTECTED]
> Subject: RE: [Hibernate] Hibernate.cfg.xml without JNDI
> 
> 
> Hmm... This is a good point. I am currently using dbcp in 
> hibernate for
> my connections, so this isn't a problem for me either. I 
> would, however,
> like to eventually move to an app server provided datasource. I wonder
> how I will run my tests then...?
> 
> Joe
> 
> > -----Original Message-----
> > From: [EMAIL PROTECTED] 
> > [mailto:[EMAIL PROTECTED] On 
> > Behalf Of Jason Carreira
> > Sent: Wednesday, February 05, 2003 3:40 PM
> > To: Raible, Matt; [EMAIL PROTECTED]
> > Subject: RE: [Hibernate] Hibernate.cfg.xml without JNDI
> > 
> > 
> > You've got the hibernate.cfg.xml using a Datasource bound in 
> > JNDI, so it won't work without JNDI. In my case, we're 
> > getting the connection ourselves from the Datasource because 
> > we're wrapping it with a proxy, so we can just create our 
> > connection in our test case.
> > 
> > Jason
> > 
> > > -----Original Message-----
> > > From: Raible, Matt [mailto:[EMAIL PROTECTED]
> > > Sent: Wednesday, February 05, 2003 2:53 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: RE: [Hibernate] Hibernate.cfg.xml without JNDI
> > > 
> > > 
> > > I tried using this, but I'm getting the following exception:
> > > 
> > >     [junit] DEBUG [main] ServiceLocator.<init>(44) | Looking
> > > up Session in JNDI
> > >     [junit] INFO [main] ServiceLocator.<init>(49) | error 
> > > communicating with JNDI, assuming testcase
> > > 
> > >     [junit] FATAL [main]
> > > DatasourceConnectionProvider.configure(44) | Could not find 
> > > datasource: jav a:comp/env/jdbc/cctdb
> > >     [junit] javax.naming.NameNotFoundException: No object 
> > > bound for java:comp/env/jdbc/cctdb
> > > 
> > > Here's my ServiceLocator's constructor:
> > > 
> > > private ServiceLocator() throws HibernateException, SQLException {
> > > 
> > >   // Try to lookup a JNDI Connection
> > >   try {
> > >           if (log.isDebugEnabled()) {
> > >                   log.debug("Looking up Session in JNDI");
> > >           }
> > >           sf = (SessionFactory) new
> > > InitialContext().lookup(Constants.SESSION_FACTORY);
> > >   } catch (NamingException ne) {
> > >           if (log.isDebugEnabled()) {
> > >                   log.info("error communicating with 
> > > JNDI, assuming testcase");
> > >           }
> > >           
> > >           sf = new
> > > Configuration("/hibernate.cfg.xml").configure()[0];
> > >           /*
> > >           Datastore datastore = Hibernate.createDatastore();
> > > 
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .ChangeRequest
> > > .class);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .ChangeRequest
> > > Detail.class);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .CMCFChannelMa
> > > p.class);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .CMCFIrt.class
> > > );
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .CMCFMaster.cl
> > > ass);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .CMCFMps.class
> > > );
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .Headend.class
> > > );
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .HSCRFMaster.c
> > > lass);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .HSCRFSignal.c
> > > lass);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .MSO.class);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .Role.class);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .User.class);
> > >   
> > > datastore.storeClass(com.comcast.cable.dmc.itd.cct.persistence
> > > .ValidationRes
> > > ults.class);
> > > 
> > >           sf = datastore.buildSessionFactory();
> > >           */
> > >   }
> > > }
> > > 
> > > And my hibernate.cfg.xml:
> > > 
> > > <?xml version='1.0' encoding='utf-8'?>
> > > <!DOCTYPE hibernate-configuration PUBLIC
> > >         "-//Hibernate/Hibernate Configuration DTD//EN"
> > >         
> > > "http://hibernate.sourceforge.net/hibernate-configuration.dtd";>
> > > 
> > > <hibernate-configuration>
> > >     <!-- shared properties -->
> > >     <property name="show_sql">false</property>
> > >     <property name="use_outer_join">true</property>
> > >     <property name="jta.UserTransaction">
> > >         java:comp/UserTransaction/
> > >     </property>
> > > 
> > >     <!-- a SessionFactory instance listed as /jndi/name -->
> > >     <session-factory name="hibernate/sessionFactory">
> > > 
> > >         <!-- properties for this SessionFactory only -->
> > >         <property
> > > name="connection.datasource">java:comp/env/jdbc/cctdb</property>
> > >         <property 
> > > name="dialect">cirrus.hibernate.sql.OracleDialect</property>
> > > 
> > >         <!-- mapping files -->
> > >         <mapping
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/ChangeRequ
> > > est.hbm.xml"/>
> > >         <mapping 
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/ChangeRequ
> > > estDetail.hbm.
> > > xml"/>
> > >         <mapping 
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/CMCFChanne
> > > lMap.hbm.xml"/
> > > >
> > >         <mapping
> > > 
> > 
> resource="com/comcast/cable/dmc/itd/cct/persistence/CMCFIrt.hbm.xml"/>
> > >         <mapping 
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/CMCFMaster
> > > .hbm.xml"/>
> > >         <mapping 
> > > 
> > 
> resource="com/comcast/cable/dmc/itd/cct/persistence/CMCFMps.hbm.xml"/>
> > >         <mapping 
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/HSCRFMaste
> > > r.hbm.xml"/>
> > >         <mapping 
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/HSCRFSigna
> > > l.hbm.xml"/>
> > >         <mapping 
> > > 
> > 
> resource="com/comcast/cable/dmc/itd/cct/persistence/Headend.hbm.xml"/>
> > >         <mapping 
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/MSO.hbm.xml"/>
> > >         <mapping 
> > > 
> resource="com/comcast/cable/dmc/itd/cct/persistence/Role.hbm.xml"/>
> > >         <mapping 
> > > 
> resource="com/comcast/cable/dmc/itd/cct/persistence/User.hbm.xml"/>
> > >         <mapping 
> > > resource="com/comcast/cable/dmc/itd/cct/persistence/Validation
> > > Results.hbm.xm
> > > l"/>
> > >         
> > >     </session-factory>
> > > 
> > > </hibernate-configuration>
> > > 
> > > 
> > > 
> > > 
> > > > -----Original Message-----
> > > > From: Jason Carreira [mailto:[EMAIL PROTECTED]
> > > > Sent: Wednesday, February 05, 2003 11:41 AM
> > > > To: Joseph Fifield; [EMAIL PROTECTED]
> > > > Subject: RE: [Hibernate] Hibernate.cfg.xml without JNDI
> > > > 
> > > > 
> > > > Hi Joseph,
> > > > 
> > > > Thank you very much! This works great! We're using a static
> > > member var
> > > > to hold the SessionFactory that is initialized in a static
> > > block. Is
> > > > this a common pattern for saving the SessionFactory?
> > > > 
> > > > Thanks again,
> > > > 
> > > > Jason
> > > > 
> > > > > -----Original Message-----
> > > > > From: Joseph Fifield [mailto:[EMAIL PROTECTED]
> > > > > Sent: Wednesday, February 05, 2003 12:21 PM
> > > > > To: [EMAIL PROTECTED]
> > > > > Subject: RE: [Hibernate] Hibernate.cfg.xml without JNDI
> > > > > 
> > > > > 
> > > > > Yes, it is possible. Something like this should do the trick:
> > > > > 
> > > > > factory = new 
> > Configuration("/hibernate.cfg.xml").configure()[0];
> > > > > 
> > > > > Joe
> > > > > 
> > > > > > -----Original Message-----
> > > > > > From: [EMAIL PROTECTED]
> > > > > > [mailto:[EMAIL PROTECTED] 
> > On Behalf 
> > > > > > Of Jason Carreira
> > > > > > Sent: Wednesday, February 05, 2003 12:13 PM
> > > > > > To: [EMAIL PROTECTED]
> > > > > > Subject: [Hibernate] Hibernate.cfg.xml without JNDI
> > > > > > 
> > > > > > 
> > > > > > Is it possible to use hibernate.cfg.xml without 
> JNDI? It would
> > > > > > even be enough if Hibernate.configure() would return the 
> > > > > > SessionFactory in addition to binding it to the JNDI 
> > > context, or
> > > > > > make it optional.
> > > > > > 
> > > > > > The reason is, I'm trying to implement Junit tests that only
> > > > > > depend upon getting a database connection, without a 
> > container 
> > > > > > (=JNDI provider) running. I tried using Mock objects, 
> > but their 
> > > > > > MockContext doesn't seem to actually store the bound 
> > > objects for
> > > > > > lookup later. I don't really want to have to 
> implement my own
> > > > > > in-memory Context, so I'm wondering why this form of 
> > > > > > configuration, by far the easiest, is so limited in 
> > what it can 
> > > > > > do?
> > > > > > 
> > > > > > Thanks,
> > > > > > 
> > > > > > Jason Carreira
> > > > > > 
> > > > > > --
> > > > > > Jason Carreira
> > > > > > Software Architect, Notiva Corp.
> > > > > > phone:      585.240.2793
> > > > > >   fax:      585.272.8118
> > > > > > email:      [EMAIL PROTECTED]
> > > > > > ---
> > > > > > Notiva - optimizing trade relationships (tm)
> > > > > >  
> > > > > > 
> > > > > > 
> > > > > > -------------------------------------------------------
> > > > > > This SF.NET email is sponsored by:
> > > > > > SourceForge Enterprise Edition + IBM + LinuxWorld
> > > =omething 2 See!
> > > > > > http://www.vasoftware.com
> > > > > > _______________________________________________
> > > > > > hibernate-devel mailing list 
> > > [EMAIL PROTECTED]
> > > > > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> > > > > > 
> > > > > > 
> > > > > 
> > > > > 
> > > > > 
> > > > > 
> > > > > -------------------------------------------------------
> > > > > This SF.NET email is sponsored by:
> > > > > SourceForge Enterprise Edition + IBM + LinuxWorld = 
> Something 2 
> > > > > See! http://www.vasoftware.com 
> > > > > _______________________________________________
> > > > > hibernate-devel mailing list 
> > [EMAIL PROTECTED]
> > > > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> > > > > 
> > > > 
> > > > 
> > > > -------------------------------------------------------
> > > > This SF.NET email is sponsored by:
> > > > SourceForge Enterprise Edition + IBM + LinuxWorld =
> > > Something 2 See!
> > > > http://www.vasoftware.com
> > > > _______________________________________________
> > > > hibernate-devel mailing list 
> [EMAIL PROTECTED]
> > > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> > > > 
> > > 
> > > 
> > > 
> > > -------------------------------------------------------
> > > This SF.NET email is sponsored by:
> > > SourceForge Enterprise Edition + IBM + LinuxWorld = Something
> > > 2 See! http://www.vasoftware.com 
> > > _______________________________________________
> > > hibernate-devel mailing list [EMAIL PROTECTED]
> > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> > > 
> > 
> > 
> > -------------------------------------------------------
> > This SF.NET email is sponsored by:
> > SourceForge Enterprise Edition + IBM + LinuxWorld =omething 2 See!
> > http://www.vasoftware.com
> > _______________________________________________
> > hibernate-devel mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> > 
> > 
> 
> 
> 
> 
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
> http://www.vasoftware.com
> _______________________________________________
> hibernate-devel mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> 



-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel


Reply via email to