struts1.1+weblogic7.0+hibernate2.02的问题

03-08-25 chenkl
我将SessionFactory bind到wls的jndi树上,在web层(struts的plugin中)可以lookup到,但在ejb层却loopup 不到,我的代码如下:

//将 SessionFactory绑定到wls的jndi上

private static void initSessionFactory() throws HibernateException{

SessionFactory sf = new Configuration().configure().buildSessionFactory();

try {

if (context == null) {

context = getInitialContext();

}

context.bind(GlobalNames.SESSION_FACTORY, sf);

}

catch (NamingException ex) {

System.out.println("init SessionFactory failed! ");

}

}

//这个方法使用weblogic的datasource,来返回session

public static Session getSession() throws HibernateException {

Connection conn = null;

SessionFactory factory = null;

try {

conn = DBUtil.getConnection();

}

catch (NoConnectionException ex) {

}

try {

if (context == null) {

context = getInitialContext();

}

factory = (SessionFactory) context.lookup(GlobalNames.SESSION_FACTORY);

}

catch (NamingException ex1) {

}

if(factory == null)

System.out.println("Can't lookup SessionFactory form jndi tree");

Session session = factory.openSession(conn);

return session;

}

//在struts的plugin中用上述的getSession方法是可以lookup到的,也可以返回session

private void initSF() {

NJSSSF sf = new NJSSSF();

try {

sf.init();

Session s1 = sf.getSession();

System.out.println("get session from plugin :(1)- " + s1.toString());

s1.close();

}

catch (HibernateException ex) {

if(GlobalNames.DEBUG_OUTPUT_FLAG){

System.out.println(ex.toString());

}

}

}

但在ejb层用getSession()方法却找不到绑定在wls jndi 上的SessionFactory,但却可以lookup到ejb,请高手指教?

猜你喜欢