数据源JNDI

Tomcat中mysql的JNDI
    1,MYSQL中数据库、表、用户名、密码的设置;

    2.a, meta-inf文件夹下新建context.xml文件,内容同下,(2.a、2.b只要设置一种即可)。

<Context>
<Resource
      name="jdbc/TestDB"
      type="javax.sql.DataSource"      
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="100"
      maxWait="30"
      username="testuser"
      password="test"
      url="jdbc:mysql://localhost:3306/testdb"
      maxActive="4"/>      
</Context>

    2.b,TOMCAT中 $CATALINA_BASE/conf/context.xml下添加  

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive
="100" maxIdle="30" maxWait="10000"
username
="testuser" password="test" driverClassName="com.mysql.jdbc.Driver"
url
="jdbc:mysql://localhost:3306/testdb"/>


    其中testdb为数据库名。
    理论上应该可以在server.xml中进行同样的设置,但是实验结果好像无法读出,不清楚原因。
     http://localhost:8888/docs/config/context.html

    3,web应用中的web.xml中,添加
       

 <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        </resource-ref>

  或者在代码中用

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

DataSource ds = (DataSource)envCtx.lookup("jdbc/TestDB");

 

jBOSS中配置JNDI数据源

https://community.jboss.org/wiki/ConfigDataSources
1,将相应的jdbc.jar复制到对应server/lib下;
2,制作xxx-ds.xml,放在deploy下。
 如果xxx-ds.xml中设置:<use-java-context>false</use-java-context>,作用:to enable external applications access to the JNDI you set up
 则ctx.lookup("MySqlDS");//这样查找数据源,不要 lookup("java:MySqlDS");

 

关于context(摘自jboss3.0文档)

Context elements may be explicitly defined:

1,In the $CATALINA_BASE/conf/context.xml file: the Context element information will be loaded by all webapps.
  2,In the $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all

webapps of that host.
  3,In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file

(less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a

context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
4,Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file

at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be

copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it

will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.
5,Inside a Host element in the main conf/server.xml.For JBoss Web, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements

directly in the server.xml file. This is because it makes modifing the Context configuration more invasive since the main

conf/server.xml file cannot be reloaded without restarting JBoss Web.

posted @ 2012-01-29 13:39  lastren  阅读(468)  评论(0编辑  收藏  举报