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