`

初遇webLogic的问题

阅读更多
今天想通过weblogic的JDBC数据连接池,数据源对数据操作,可是遇到了问题。
首先在控制台想测试一下是否连接成功。
下面是控制给出的提示:

在此页中,您可以在特定服务器上或部署 JDBC 连接缓冲池的所有服务器上测试此 JDBC 连接缓冲池。

连接缓冲池测试使用在 "配置" > "连接" 选项卡 "高级选项" 中配置的测试选项。必须为 "测试表名称" 指定一个值并选择 "测试保留的连接" 或 "测试释放的连接" 以测试连接缓冲池。

测试保留的连接和测试释放的连接任意选一个就可以测试了。

好,现在控制台的测试通过了。现在我又新建了一个类,通过获取上下文来获取连接源。
代码:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.*;
import javax.sql.DataSource;

public class JDBCPoolWeblogic {
	// Defines the JNDI context factory.
	public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

	// Defines the JNDI provider url.
	public final static String PROVIDER_URL = " t3://localhost:7001";
	
	public static void main(String[] args) throws Exception
	{
		Connection myConn = null;
		DataSource ds = null;
		Context ctx = getInstance();
		ds = (javax.sql.DataSource) ctx.lookup("MYPoolYLJNDI");
		myConn = ds.getConnection();
		Statement stmt = myConn.createStatement();
		ResultSet rs = stmt.getResultSet();
		rs = stmt.executeQuery("select * from AAA");
		if (rs.next()) {
			System.out.print(rs.getString("ABC"));
		}
	}

	public static Context getInstance() throws Exception 
	{
		Properties properties = null;
		String url = "t3://localhost:7001";
		String user = "weblogic";//weblogic登陆的用户名和密码
		String password = "weblogic";
		try 
		{
			properties = new Properties();
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
			properties.put(Context.PROVIDER_URL, url);
			if (user != null) 
			{
				properties.put(Context.SECURITY_PRINCIPAL, user);
				properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
			}
			return new InitialContext(properties);
		}
		catch (Exception e) 
		{
			throw e;
		}
	}
}

问题是运行到myConn = ds.getConnection();这句话的时候出问题了
java.lang.reflect.InvocationTargetException
--------------- nested within: ------------------
weblogic.utils.AssertionError: ***** ASSERTION FAILED *****[ Failed to generate class for weblogic.jdbc.rmi.internal.ConnectionImpl_weblogic_jdbc_wrapper_PoolConnection_oracle_jdbc_driver_T4CConnection_813_WLStub ] - with nested exception:
[java.lang.reflect.InvocationTargetException - with target exception:
[java.lang.ArrayIndexOutOfBoundsException: 211]]

找道原因了取数据源只能在容器中取,马上换成servlet+jsp访问.就可以取到。
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Context ctx=null;
		DataSource ds=null;
		Connection conn=null;
		response.setContentType("text/html;charset=gbk");
		PrintWriter out=response.getWriter();
		ResultSet rs=null;
		Statement st=null;
		try {
			ctx=JDBCPoolWeblogic.getInstance();
			ds=(DataSource)ctx.lookup("MYPoolYLJNDI");
			conn=ds.getConnection();
			st=conn.createStatement();
			rs=st.executeQuery("select cityname from city");
			rs.next();
			String cityname=rs.getString("cityname");
			out.println("<html>");
			out.println("<body>");
			out.println("<h1>"+cityname+"</h1>");
			out.println("</body>");
			out.println("</html>");
			out.flush();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			out.close();
			try {
				rs.close();
				st.close();
				conn.close();				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics