开发工具:Myeclipse
服务容器:weblogic 8
连接方式:获取weblogic的datasource每次开发做单元测试,添加某个数据库操作方法后。总是需要走重新登录(再次开发,所有没有屏蔽登录超时、session变更等验证。这不是这个问题的要点)。有次自己做了个测试。在dao操作类中添加一个main方法,直接调用新增的方法。
在服务未启动时抛出未找到datasource异常。然后尝试的启动服务后,直接运行该main。直接通过,当前dao类的main可以访问操作数据库。对main方法做了多次的调整测试。今天再次运行改main的时候,却无法再次获取datasource。尝试了N种方式都无法实现之前的操作。在线等了,100分送上。谢谢!
服务容器:weblogic 8
连接方式:获取weblogic的datasource每次开发做单元测试,添加某个数据库操作方法后。总是需要走重新登录(再次开发,所有没有屏蔽登录超时、session变更等验证。这不是这个问题的要点)。有次自己做了个测试。在dao操作类中添加一个main方法,直接调用新增的方法。
在服务未启动时抛出未找到datasource异常。然后尝试的启动服务后,直接运行该main。直接通过,当前dao类的main可以访问操作数据库。对main方法做了多次的调整测试。今天再次运行改main的时候,却无法再次获取datasource。尝试了N种方式都无法实现之前的操作。在线等了,100分送上。谢谢!
Looking up the data source,exception :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
java.lang.NullPointerException
at cfchina.ccb.cdse.db.DBAccessDatabase.getConnection(DBAccessDatabase.java:86)
at cfchina.ccb.cdse.db.basedao.BaseDaoService.queryConf(BaseDaoService.java:918)
at cfchina.ccb.cdse.db.basedao.BaseDaoService.main(BaseDaoService.java:960)
Exception in thread "main"
================================================================================贴下DBAccessDatabase.getConnection()这个方法。
public Connection getConnection() {
javax.sql.DataSource ds = null;
Properties properties = null;
String user = null;
String password = null; Connection conn = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:8080");
//properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
} //Context ctx = new InitialContext(properties);
Context ctx = new InitialContext();
// ds = (javax.sql.DataSource) ctx.lookup(Constants.JNDI_DATASOURCE_TOMCAT);
ds = (javax.sql.DataSource) ctx.lookup(Constants.JNDI_DATASOURCE_WL);
// ds = (javax.sql.DataSource)initCtx.lookup(Constants.JNDI_DATASOURCE_WL_TEST );
System.out.println("*******get Connection successful!************");
} catch (javax.naming.NamingException ne) {
System.out.println("Looking up the data source,exception :"
+ ne.getMessage());
} catch (Exception e) {
System.out.println("*********get conn fail************************");
}
try {
conn = ds.getConnection();
} catch (SQLException sqle) {
System.out.println("Getting the connection,exception :"
+ sqle.getMessage());
}
return conn;
}这是个N年前的项目。这个异常无非就是无法找到datasource。
问过好多人,他们基本都没这种方式做个测试。第一反应都是这种方式无法访问数据库。说我那次的访问成功属于我的幻觉#83 狂汗
你确认weblogci平台上面数据库测试是正常通过的吗?
你确认你的weblogic对外提供的服务端口是8080吗
Looking up the data source,exception :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
java.lang.NullPointerException
at cfchina.ccb.cdse.db.DBAccessDatabase.getConnection(DBAccessDatabase.java:86)
1.看不到行号
2.你的属性文件是否哪里写错了? 异常信息有说或者是空指针
另外 8080是tomcat的默认端口 weblogic的默认端口是7001
谢谢问题是在,启动服务后。通过IE可以正常的访问项目及正常操作数据库
但是我在dao类中的main直接调用dao的方法,却出现该错误,所以很让我不理解。dao中的方法是最小单元功能的实现,不牵扯到任何的权限等等方面的限制。
conn = ds.getConnection();这一句这句回调该方法。跟踪下去,是在ds = (javax.sql.DataSource) ctx.lookup(Constants.JNDI_DATASOURCE_WL);这一句,关键还是通过JNDI名无法加载到该DataSource空指针指的就是这个。
"org.apache.naming.java.javaURLContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:8080");
看这里 你获取的并不是weblogic的jndi 因为端口是8080
也是不是tomcat的jndi 因为t3://localhost: 是weblogic对外的通的访问方式..
public Connection getConnection() {
javax.sql.DataSource ds = null;
Properties properties = null;
String user = null;
String password = null; Connection conn = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:8080");
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
} Context ctx = new InitialContext();
ds = (javax.sql.DataSource) ctx.lookup(Constants.JNDI_DATASOURCE_WL);
System.out.println("*******get Connection successful!************");
} catch (javax.naming.NamingException ne) {
System.out.println("Looking up the data source,exception :"
+ ne.getMessage());
}
try {
conn = ds.getConnection();
} catch (SQLException sqle) {
System.out.println("Getting the connection,exception :"
+ sqle.getMessage());
}
return conn;
}
我初试上下文Context 的时候 并不是
Context ctx = new InitialContext(properties);
而是用的
Context ctx = new InitialContext();
原先是获取tomcat的方式 所以是通过 properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:8080");
Context ctx = new InitialContext(properties);
ds = (javax.sql.DataSource) ctx.lookup(Constants.JNDI_DATASOURCE_TOMCAT);而我现在项目中实际上仅仅只是这样实现的,设置的properties并没有使用到public Connection getConnection() {
javax.sql.DataSource ds = null;
Properties properties = null;
String user = null;
String password = null; Connection conn = null;
try {
Context ctx = new InitialContext();
ds = (javax.sql.DataSource) ctx.lookup(Constants.JNDI_DATASOURCE_WL);
} catch (javax.naming.NamingException ne) {
System.out.println("Looking up the data source,exception :"
+ ne.getMessage());
}
try {
conn = ds.getConnection();
} catch (SQLException sqle) {
System.out.println("Getting the connection,exception :"
+ sqle.getMessage());
}
return conn;
}
没细读这段代码,不好意思了各位。问题回归,这样的实现。我直接执行main无法通过正确的JNID名称获取DataSource
原系统环境
JDK 1.4.2
WEBLOGIC 8.1
STRUTS 1
==================================================
没有使用spring PM也不打算做架构重构