-->
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.  [ 3 posts ] 
Author Message
 Post subject: JNDI lookup in a standalone application
PostPosted: Tue Feb 08, 2005 4:20 am 
Newbie

Joined: Tue Feb 08, 2005 3:30 am
Posts: 6
Location: Bangalore
Hello There!

I have a class called monitor which monitors the product inventory and sends an email to Admin if the inventory is below threshold.

Basically, this (client) program runs fine in the web-context. I instantiated the client class in one of the action class to make sure that it works. Here is how I tested it.

Code:
         // testing cron here
        com.lbkgroup.cron.InventoryMonitor i = new com.lbkgroup.cron.InventoryMonitor();
        i.cron();
       com.lbkgroup.monitor.SubscriptionMonitor sm  = new com.lbkgroup.monitor.SubscriptionMonitor();
        sm.subscriptionMonitor();



So far so good, everything worked fine as expected.

now I changed the method cron to main(String[]) so that I can run in from the shell and later make it as a cron job.

Then the problem started. and this is what I get when I try to run the client program.

In the jar file that I deployed the manifest file has
Main-Class : com.lbkgroup.cron.InventoryMonitor

And when I try to run it as java -jar App.jar , it gives me

SEVERE: Could not find datasource: java:comp/env/PostgresJNDIRef
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

SEVERE: Initial SessionFactory creation failed.
net.sf.hibernate.HibernateException: Could not find datasource

Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

My hibernate.cgf.xml file is as below
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/PostgresJNDIRef</property>
        <property name="show_sql">false</property>
        <property name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>
        <property name="transaction.factory_class"> net.sf.hibernate.transaction.JTATransactionFactory </property>
        <!-- Mapping files -->
        <mapping resource="com/lbkgroup/businessobjects/Administrator.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/Catalog.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/Category.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/Customer.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/Address.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/Vendor.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/Warehouse.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/Product.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/SalesOrder.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/SalesOrderLineItem.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/SubLineItem.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/PurchaseOrder.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/PurchaseOrderLineItem.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/SalesOrderPayment.hbm.xml"/>
        <mapping resource="com/lbkgroup/businessobjects/SalesOrderRefund.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


and I get the reference to the session thru a util class as follows.

Code:
public class HibernateUtil {

        private static Log log = LogFactory.getLog(HibernateUtil.class);

        private final static 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);
                }
        }

        /**
         * <code>session</code> is a hibernate session.
         */
        public final static ThreadLocal session = new ThreadLocal();
        /**
         * <code>currentSession</code> is a currently active session.
         *
         * @return                         a <code>Session</code> value
         * @exception  HibernateException  if an error occurs
         */
        public static Session currentSession() throws HibernateException {
                Session s = (Session) session.get();
                // Open a new Session, if this Thread has none yet
                if (s == null) {
                        s = sessionFactory.openSession();
                        session.set(s);
                }
                return s;
        }


        /**
         * <code>closeSession</code> closes the current session.
         *
         * @exception  HibernateException  if an error occurs
         */
        public static void closeSession() throws HibernateException {
                Session s = (Session) session.get();
                session.set(null);
                if (s != null) {
                        s.close();
                }
        }
}



Please help!


Thanks
Sri


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 10, 2005 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Usually a datasource is in your J2EE app server, Please refer to the documentation to set your favorite connection provider (C3P0 for example)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 10, 2005 6:28 pm 
C3P0 Developer
C3P0 Developer

Joined: Tue Jan 06, 2004 8:58 pm
Posts: 145
Hi.

Before you can construct a JNDI InitialContext object, you need to set up some configuration parameters, wither directly in your code, or via a jndi.properties file. The most important of these parameters are java.naming.factory.initial and java.naming.provider.url

You can sometimes find the appropriate config to access your application by making sure any jndi.properties file in your app server's classpath is also in your standalone app's classpath (along with your appserver's jndi implementation's jar file). You may find information on how to access your appserver's jndi implementation from a standalone app in your appserver's documentation.

See http://java.sun.com/products/jndi/tutor ... ource.html for more information.

Steve


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