tomcat配置全局JNDI和私有jndi的方式

2019-03-12 22:17|来源: 未知

全局JNDI的配置

在server.xml里面的<GlobalNamingResources>标签里面添加如下配置:

<Resource name="jdbc/test" auth="Container"

type="javax.sql.DataSource"

driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://127.0.0.1/test"

username="root"

password="root"

maxActive="20"

maxIdle="10"

maxWait="-1"/>

在项目里引用此全局JNDI的方法

在项目的META-INF下面建立context.xml文件,添加以下内容

<?xml version="1.0" encoding="UTF-8"?>

<Context>

   <ResourceLink name="jdbc/test" global="jdbc/test" type="javax.sql.DataSource"/>

</Context>

然后就可能在程序里面通过context.lookup("java:comp/env/jdbc/test")进行访问了。


私有的JNDI的配置

1、可以直接在server.xml里面的<Host>节点下添加如下配置

<Context path="/test_tomcat6_jndi">

<Resource name="jdbc/test"

      auth="Container"

             type="javax.sql.DataSource"

             driverClassName="com.mysql.jdbc.Driver"

             url="jdbc:mysql://127.0.0.1/test"

             username="root"

             password="root"

             maxActive="20"

             maxIdle="10"

             maxWait="-1"/>

</Context>


使用此JNDI的方法:

在程序中通过context.lookup("java:comp/env/jdbc/test")访问了

注:path="/test_tomcat6_jndi",这个名字必须和你的项目名称相同,而且不能少了那个斜杠,而且你的项目是通过拷贝文件夹到webapps下面的方式进行的部署。


2、 也可以在conf/context.xml里面增加如下配置:

<Resource name="jdbc/test" auth="Container"

 type="javax.sql.DataSource"

 driverClassName="com.mysql.jdbc.Driver"

 url="jdbc:mysql://127.0.0.1/test"

 username="root"

 password="root"

 maxActive="20"

 maxIdle="10"

 maxWait="-1"/>

这样就可以直接在程序中通过context.lookup("java:comp/env/jdbc/test")访问了

3、还可以在项目的WebRoot下面的META-INF文件夹下面创建context.xml文件,再在context.xml文件中添加以下内容

<?xml version="1.0" encoding="UTF-8"?>

<Context>

    <Resource name="jdbc/test"

  auth="Container"

             type="javax.sql.DataSource"

             driverClassName="com.mysql.jdbc.Driver"

             url="jdbc:mysql://127.0.0.1/test"

             username="root"

             password="root"

             maxActive="20"

             maxIdle="10"

             maxWait="-1"/>

</Context>

这样就可以直接在程序中通过context.lookup("java:comp/env/jdbc/test")访问了


整理自网络


相关问答

更多
  • factory org.apache.commons.dbcp.BasicDataSourceFactory url jdbc:mysql:// localhost/demo driverClassName com.mysql.jdbc.Driver password 12345 username admin maxIdel 0 maxActive 100
  • 我常用的web项目METE-INF下添加,修改方便些 <?xml version="1.0" encoding="UTF-8" ?> - <Context> <Resource name="o10g" type="javax.sql.DataSource" password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver" maxIdle="5" maxWait="5000" username="scott" u ...
  • 最简单的办法是通过tomcat的manager界面来进行配置
  • 代码: /** * 获取数据库连接方法 */ public Connection getConn() throws Exception { Connection conn = null; try { //connection原始的连接方式可以转换为OracleConnection Connection con = this.commonDao.getCurrentConnection(); Connection conWas = null; if(this.wasConnection){//使用tomcat ...
  • 范围只在tomcat的JVM里面,在普通的java类里写一个main不能调到!但你可以构造一个tomcat的JVM
  • 看起来像你在tomcat中运行应用程序的堆栈跟踪,所以请确保以下几点: 1)你的pom.xml或项目特定的lib文件夹中不应该有任何jta,bitronix相关的jar,因为这些jar应该在你的tomcat lib文件夹中。 2)根据文档中的这两个文件存在于tomcat的conf文件夹中,同时更改server.xml和context.xml。 我不确定什么是您的配置jndi lookup.You可以通过下面的链接,这是为tomcat设置bitronix。 https://github.com/bitroni ...
  • 我相信JNDI属性应该是供应商特定的,你试图放置的是Jboss JNDI属性,并且可能已经恢复为Tomcat特定验证。 尽管您可以尝试以编程方式覆盖它们,但它可能无法正常工作。 I believe the JNDI properties should be vendor specific, What you trying to put is for Jboss JNDI properties, and may have been reverting to for Tomcat Specific Proert ...
  • 这是一个链接http://community.jboss.org/wiki/UsingJNDI-boundSessionFactorywithTomcat41 但也欢迎其他答案。 Here's a link http://community.jboss.org/wiki/UsingJNDI-boundSessionFactorywithTomcat41 But other answers are welcome as well.
  • 没有'JNDI服务器'这样的东西。 JNDI是一种客户端API,可与各种命名技术相连接。 您可以考虑使用LDAP服务器。 There is no such thing as a 'JNDI server'. JNDI is a client-side API that interfaces to various naming technologies. You could consider an LDAP server.
  • 将您的数据源配置移至server.xml 。 (并且不要忘记将驱动程序jar文件添加到tomcat lib)。 配置您的context.xml,以便所有应用程序都知道全局资源。 WEB-INF/web.xml