获得jboss数据源 <use-java-context>false</use-java-context>的问题

原文地址: 关于JBOSS数据源配置 <use-java-context>false</use-java-context>的问题

数据源定义文件**-ds.xml中,有个use-java-context属性,把它设为false的话,JNDIView里生成的数据源就会位于全局命名空间,没有java前缀了。

一、没有添加<use-java-context>false</use-java-context>

 <local-tx-datasource>
    <jndi-name>platform</jndi-name>
    <connection-url>jdbc:oracle:thin:@59.65.233.199:1521:GIS</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>PLATFORM</user-name>
    <password>PLATFORM</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>        
      <metadata>
         <type-mapping>Oracle9i</type-mapping>
      </metadata>
  </local-tx-datasource>

获得数据源的代码

	public Connection getConnection() {

		Connection connection = null;
		try {
			Context ctx = new InitialContext();// 得到初始化上下文
			DataSource ds = (DataSource) ctx.lookup("java:platform");// 这样查找数据源,不要lookup("platform");
			connection = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}

		return connection;
	}

 

二、添加<use-java-context>false</use-java-context>

 <jndi-name>flow</jndi-name>
    <use-java-context>false</use-java-context> 
    <connection-url>jdbc:oracle:thin:@59.65.233.199:1521:GIS</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>FLOW</user-name>
    <password>FLOW</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>        
 

获得数据源的代码

	public Connection getConnection1() {
		Connection connection = null;
		try {
			Context ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("flow");
			connection = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}

		return connection;
	}

 

三、如果持久层使用了EJB,对应的persistence.xml配置文件为:

<persistence>
	<persistence-unit name="test">
		<jta-data-source>java:/platform</jta-data-source>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
			<property name="com.arjuna.ats.jta.allowMultipleLastResources"
				value="true" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

	<persistence-unit name="test1">
		<jta-data-source>flow</jta-data-source>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
			<property name="com.arjuna.ats.jta.allowMultipleLastResources"
				value="true" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>
</persistence>

四、如果想跨JVM的话,需要有<use-java-context>false</use-java-context>,

这样做的好处就是解决了不在同一VM里导致的javax.naming.NameNotFoundException: *** not bound 异常。

新建一个类 TestDS.java 运行,此时,它们不在一个JVM中

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class TestDS {

	public static void main(String[] args) {

		Properties prop = new Properties();
		prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		prop.setProperty(Context.PROVIDER_URL, "localhost:1099");
		InitialContext context = null;
		DataSource dataSource = null;
		try {
			context = new InitialContext(prop);
			//有<use-java-context>false</use-java-context> 在不同JVM可以访问,以下两种方式都可以
			dataSource = (DataSource) context.lookup("flow");
			dataSource = (DataSource) context.lookup("java:flow");
			System.out.println(dataSource);//
			
			//没有<use-java-context>false</use-java-context>
			//报异常 javax.naming.NameNotFoundException: platform not bound
			dataSource = (DataSource) context.lookup("platform");//java:platform 也一样
			System.out.println(dataSource);
		} catch (NamingException e) {
			e.printStackTrace();
		}

	}
}

代码如下:运行,报异常

javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory

添加 jboss-4.2.3.GA\client\jbossall-client.jar后即可运行。
 

 

posted on 2013-03-11 11:26  小-强-斋-太  阅读(189)  评论(0编辑  收藏  举报

导航