tomcat下JNDI的三種配置方式(轉載+原創)

        JNDI(Java Naming and Directory Interface,Java命名和目錄接口)是一組在Java應用中訪問命名和目錄服務的API。命名服務將名稱和對象聯繫起來,使得咱們能夠用名稱訪問對象。目錄服務是一種命名服務,在這種服務裏,對象不但有名稱,還有屬性。html

        tomcat配置jndi有全局配置和局部配置。大體的有如下三種配置方式:java

 第一種:全局配置。mysql

1)在tomcat的conf文件夾下的context.xml配置文件中加入:web

<Resource name="jndi/mybatis" 
	auth="Container"
	type="javax.sql.DataSource"
	driverClassName="com.mysql.jdbc.Driver"
	url="jdbc:mysql://localhost:3306/appdb"
	username="root"
	password="123456"
	maxActive="20"
	maxIdle="10"
	maxWait="10000" />

2)在項目的web.xml中加入資源引用:sql

<resource-ref>
  <description>JNDI DataSource</description>
  <res-ref-name>jndi/mybatis</res-ref-name>
  <res-ref-type>javax.sql.DataSource</res-ref-type>
  <res-auth>Container</res-auth>
</resource-ref>

其中res-ref-name值要和context.xml的name值一致。數據庫

3)jndi測試方法:apache

public void testJNDI() throws NamingException, SQLException {
	Context ctx = new InitialContext();
	DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/mybatis");
	Connection conn = ds.getConnection();
	System.out.println(conn.isClosed());
}

4)在jsp中調用加載jndi方式,不能夠直接用main方法測試,必須經過啓動容器從jsp中調用:tomcat

TestPageAccessURL test = new TestPageAccessURL();  
test.testJNDI();

第二種:局部配置(不推薦)。mybatis

1)在tomcat的server.xml的<host>標籤內,添加:app

<Context path="/demo_jndi" docBase="/demo_jndi">  
   <Resource  
     name="jndi/mybatis"  
     type="javax.sql.DataSource"  
     driverClassName="com.mysql.jdbc.Driver"  
     maxIdle="2"  
     maxWait="5000"  
     username="root"  
     password="123456"  
     url="jdbc:mysql://localhost:3306/appdb"  
     maxActive="4"/>  
</Context>

其餘配置同第一種方式。

第三種:局部配置。

1)在項目的META-INFO下面新建context.xml。加入:

<?xml version="1.0" encoding="UTF-8" ?>
<Context>
	<Resource name="jndi/mybatis"
		auth="Container"
		type="javax.sql.DataSource" 
		driverClassName="com.mysql.jdbc.Driver" 
		url="jdbc:mysql://localhost:3306/appdb" 
		username="root" 
		password="123456" 
		maxActive="20" 
		maxIdle="10" 
		maxWait="10000" />
</Context>

其餘配置同第一種方式。

總結:若是要配置局部的話,推薦使用第三種方式,這樣不依賴tomcat了。可是仍是推薦使用第一種方式好,雖然依賴tomat,可是是全局的,並且能夠配置
多個。對於之後切換使用方便。
在項目的web.xml中添加的資源引用無關緊要。

 

上面是轉載的內容

下面還有另一種配置方式。

第四種:GlobalNamingResources(配置系統的JNDI(Java Naming and Directory Interface))

1)在tomcat的server.xml的<GlobalNamingResources>節點內配置,默認的內容以下:

<GlobalNamingResources>
  <!-- Editable user database that can also be used by
       UserDatabaseRealm to authenticate users
  -->
  <Resource name="UserDatabase" auth="Container"
    type="org.apache.catalina.UserDatabase"
    description="User database that can be updated and saved"
    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

在該節點中添加:

<GlobalNamingResources>
  <!-- Editable user database that can also be used by
       UserDatabaseRealm to authenticate users
  -->
  <Resource name="UserDatabase" auth="Container"
    type="org.apache.catalina.UserDatabase"
    description="User database that can be updated and saved"
    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname="conf/tomcat-users.xml" />
    
  <!--配置mysql數據庫的鏈接池, 
      須要作的額外步驟是將mysql的Java驅動類放到tomcat的lib目錄下        
  -->
  <Resource name="jdbc/mysqlpool" 
    auth="Container" 
    type="javax.sql.DataSource" 
    username="root" 
    password="root" 
    maxIdle="30" 
    maxWait="10000" 
    maxActive="100"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/test" />
    
</GlobalNamingResources>

2)在tomcat目錄下的conf/context.xml的<Context>節點中添加

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

3)在項目的web.xml中加入資源引用:

<resource-ref>
  <description>JNDI DataSource</description>
  <res-ref-name>jdbc/mysqlpool</res-ref-name>
  <res-ref-type>javax.sql.DataSource</res-ref-type>
  <res-auth>Container</res-auth>
</resource-ref>

Tomcat數據庫鏈接池的配置方法總結

相關文章
相關標籤/搜索