由于工作关系,需要在hibernate中使用weglogic的JNDI,可是从网上查到的一种实现方法,行不通。
从网上查资料,有一个方案比较可行的,就是编写一个实现T3StartupDef接口的启动类,让weblogic启动的时候加载这个类来初始化数据源,但尝试的结果是,不管怎么修改weblogic的启动文件startWebLogic.cmd,始终在启动weblogic时找不到这个类。
下面尝试利用hibernate自身的功能来实现JNDI的调用。走,看文档去啦。
对了,有哪位高手知道怎么解决这个问题可以Q我:25361549
下面是我编写的启动类
package com.midi;
import java.util.*;
import javax.naming.*;
import weblogic.common.T3StartupDef;
import weblogic.common.T3ServicesDef;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateStartUp implements T3StartupDef {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
public static final String SESSION_FACTORY_JNDI = "test/jdbc/hibernateFirst";
public static final String URL = "t3://localhost:7001";
public void setServices(T3ServicesDef services) {
}
public String startup(String name, Hashtable args) throws Exception {
String METHOD_NAME = "startup ";
try {
System.out.println(METHOD_NAME + " Going to bind Hibernate object. ");
doBind();
System.out.println(METHOD_NAME + " Bound Hibernate object!");
} catch (Exception e) {
System.out.println(METHOD_NAME
+ " Exception while binding Hibernate Object to Weblogic JNDI");
e.printStackTrace();
}
return "WLS Startup completed successfully";
}
private static void doBind() throws Exception {
Properties environment = null;
InitialContext context = null;
try {
environment = new Properties();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
environment.put(Context.PROVIDER_URL, URL);
System.out.println("Constructing an Initial Directory Context object");
context = new InitialContext(environment);
Configuration conf = new Configuration();
conf.configure(CONFIG_FILE_LOCATION);
SessionFactory sf = conf.buildSessionFactory();
if (sf == null)
throw new Exception("SessionFactory cannot be built!");
try {
if (context.lookup(SESSION_FACTORY_JNDI) != null)
context.rebind(SESSION_FACTORY_JNDI, sf);
else
context.bind(SESSION_FACTORY_JNDI, sf);
} catch (NamingException nameEx) {
context.bind(SESSION_FACTORY_JNDI, sf);
}
} catch (NamingException nameExp) {
throw new Exception("NamingException: " + nameExp.getMessage());
} catch (Exception excp) {
throw excp;
} finally {
if (context != null) {
try {
context.close();
context = null;
} catch (NamingException nameExp) {
throw new Exception("NamingException for context close: "
+ nameExp.getMessage());
}
}
environment = null;
}
}
}