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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/dataiyangu/article/details/99871014

背景描述

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

weblogic使用JNDI

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
自行配置
在这里插入图片描述
在这里插入图片描述
往下拉这里有一个测试的表名需要注意下,可能跟下面的错误有关系,不能使oracl关键字ORA
在这里插入图片描述

在这里插入图片描述
点击刚刚新建的
在这里插入图片描述
在这里插入图片描述
往下拉下面有一个高级点开
在这里插入图片描述
填入sql,并点击下方的保存
在这里插入图片描述
目标,点击服务器,并保存。这里如果不点会报错,下面有记录,如果保存的时候有报错,下面也有记录,不要着急,往下看。
在这里插入图片描述

测试代码

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 下午060955CST> <Warning> <netuix> <BEA-423420> <Redirect is executed in begin or refresh action. Redirect url is /console/console.portal?_nfpb=true&_pageLabel=JdbcDatasourcesJDBCDataSourceConfigTabPage&JdbcDatasourcesJDBCDataSourceConfigPortlethandle=com.bea.console.handles.JMXHandle%28%22com.bea%3AName%3Dleesin%2CType%3Dweblogic.j2ee.descriptor.wl.JDBCDataSourceBean%2CParent%3D%5Bbase_domain%5D%2FJDBCSystemResources%5Bleesin%5D%2CPath%3DJDBCResource%5Bleesin%5D%22%29.>
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.<clinit>(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
保存后即可;
在这里插入图片描述
会报如下的错误

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

猜你喜欢

转载自blog.csdn.net/dataiyangu/article/details/99871014