java weblogic连接池,Weblogic JNDI 方式连接连接池 (工作中遇到的问题)

背景描述

工作中客户的代码中没有alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS',这个sql,可是agent每条请求平均抓了60次,用户“要一个说法”

weblogic使用JNDI

b7c3ea5bb1011734f4475e465664f475.png

a70099eb26b298e148adbea393dd3a5d.png

e93404439f00a9cb3c13962394b4159b.png

95d1beb9dc7c007117d9bece501618f6.png

自行配置

a7480802cd3fb902574c57e6f7691d81.png

fc4de3b8f2b98f0b4aceda46d884c2b1.png

往下拉这里有一个测试的表名需要注意下,可能跟下面的错误有关系,不能使oracl关键字ORA

8892407dec0fde7d28a70219e84e0fa4.png

f332ac95a16a5a1a33a2e925899bc6cc.png

点击刚刚新建的

62eaa00cb8e4768cab717db7ec516ddd.png

784eccc4fb9e5e2b1edd6bc1227047e2.png

往下拉下面有一个高级点开

46cbea9cd3ff873769b2548cd685925a.png

填入sql,并点击下方的保存

22e1c412e777e815ac0fb16d6e0170db.png

目标,点击服务器,并保存。这里如果不点会报错,下面有记录,如果保存的时候有报错,下面也有记录,不要着急,往下看。

9497c48adb2fc41ec969a006a88036ae.png

测试代码

demo1(我用这个成功了)

package com.leesin;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Jndi_test {

public static void main(String[] args) {

Connection conn = null;

try {

Context ctx = new InitialContext();

//tomcat连接池

//DataSource ds=(DataSource)ctx.lookup("java:comp/env/mysql55");

//weblogic连接池

//lookup后面的是jndi的名字,在weblogic中进行配置

//必须和上面配置的一致。

DataSource ds = (DataSource) ctx.lookup("leesin");

conn = ds.getConnection();

Statement stmt6 = conn.createStatement();//创建一个Statement对象

String sql7 = "SELECT * FROM student";//生成一条sql语句

ResultSet rs17 = stmt6.executeQuery(sql7);//执行查询,把查询结果赋值给结果集对象

System.out.println("编号 \t 姓名 \t 工资 -------statment");

while (rs17.next()) {

System.out.println(rs17.getString(1) + "\t" + rs17.getString(2) + "\t" + rs17.getString(3));

}

} catch (NamingException e) {

e.printStackTrace();

} catch (

SQLException e) {

e.printStackTrace();

}

}

}

下面的demo2,private final static String PROVIDER_URL = “t3://10.0.2.90:7001”;有这句话,这里dmeo1没有问题,可能默认是localhost的,因为要部署到weblogic上运行。

demo2

package com.leesin;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

public class WebLogicJndiUtil {

//初始化上下文需要用到的工厂类

private final static String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";

//WebLogic服务器的访问地址

private final static String PROVIDER_URL = "t3://10.0.2.90:7001";

//WebLogic服务器中的JNDI数据源名称

//private final static String ORACLE_JNDI_NAME = "JNDI/OracleDataSource";

private final static String ORACLE_JNDI_NAME = "leesin";

private final static String MYSQL_JNDI_NAME = "JNDI/MysqlDataSource";

//存储从JNDI容器中取出来的数据源

private static DataSource dsOracle = null;

private static DataSource dsMySQL = null;

static {

try {

//初始化WebLogic Server的JNDI上下文信息

Context context = getInitialContext();

//获取数据源对象

dsOracle = (DataSource) context.lookup(ORACLE_JNDI_NAME);

dsMySQL = (DataSource) context.lookup(MYSQL_JNDI_NAME);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* MethodName: getInitialContext

* Description: 获得WebLogic ServerJNDI初始上下文信息

*

* @return

* @throws Exception

* @author xudp

*/

private static Context getInitialContext() throws Exception {

Properties properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);

properties.put(Context.PROVIDER_URL, PROVIDER_URL);

return new InitialContext(properties);

}

/**

* MethodName: getOracleConnection

* Description: 获取Oracle数据库连接

*

* @return

* @throws SQLException

* @author xudp

*/

public static Connection getOracleConnection() throws SQLException {

return dsOracle.getConnection();

}

/**

* MethodName: getMySQLConnection

* Description: 获取MySQL数据库连接

*

* @return

* @throws SQLException

* @author xudp

*/

public static Connection getMySQLConnection() throws SQLException {

return dsMySQL.getConnection();

}

}

然后调用这个类的getOracleConnection方法得到connection即可。

报错 javax.naming.NameNotFoundException: Unable to resolve ‘leesin’. Resolved ‘’; remaining name ‘leesin’

<2019-8-20 下午06时09分55秒 CST>

javax.naming.NameNotFoundException: Unable to resolve 'leesin'. Resolved ''; remaining name 'leesin'

at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)

at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:252)

at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:182)

at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:206)

at weblogic.jndi.internal.WLEventContextImpl.lookup(WLEventContextImpl.java:254)

at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:412)

at javax.naming.InitialContext.lookup(InitialContext.java:417)

at com.leesin.WebLogicJndiUtil.(WebLogicJndiUtil.java:29)

at com.leesin.Jndi_test.main(Jndi_test.java:23)

at com.leesin.Servlet.doGet(Servlet.java:39)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)

at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)

at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)

at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:184)

at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3732)

at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3696)

at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)

at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2273)

at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)

at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1490)

at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)

at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

解决方法:

在Weblogic数据源管理中的

JDBC Data Source-0的设置

中设置目标选项

将服务勾选上,如默认的:AdminServer

保存后即可;

c63a129c1a8cda38c153ad253c55532b.png

会报如下的错误

java.sql.SQLSyntaxErrorException: ORA-00903: 表名无效

这句话的意思是ORA-00903是oracle的关键字,不能使用,我的demo中没有使用啊,回想一下,我在建立连接池的时候,添加了一个初始化的sql,会不会是这个原因呢?

当然我上面有记录,也可能跟那个有关系。

后来换了一个weblogic就可以了,可能是版本的问题。

之后就能成功连接了。

文章:

https://blog.csdn.net/wx5040257/article/details/77926540

https://blog.csdn.net/weixin_37264997/article/details/84820316

https://blog.csdn.net/acmman/article/details/70146603

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值