您的位置:首页 > 运维架构 > Tomcat

使用JNDI配置Tomcat数据源[提高访问数据库效率]

2008-02-06 00:24 726 查看
原理:在DataSource中事先建立多个数据库连接,保存在数据库连接池中。当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池,呵呵,这与每次去直接访问数据库相比,会节省大量时间和资源。恩,感觉不错~
JNDI( Java Naming and Directory Interface ),是Java平台 的一个标准扩展,提供了一组接口、类和关于命名空间的概念。如同其它很多Java技术一样,JDNI是provider-based的技术,暴露了一个 API和一个服务供应接口(SPI)。这意味着任何基于名字的技术都能通过JNDI而提供服务,只要JNDI支持这项技术。JNDI目前所支持的技术包括 LDAP、CORBA Common Object Service(COS)名字服务、RMI、NDS、DNS、Windows注册表等等。很多J2EE技术,包括EJB都依靠JNDI来组织和定位实体。
哦~JNDI的概念好长~呵呵,朋友们可以把它理解为一种将对象和名字捆绑的技术,对象工厂负责生产出对象,这些对象都和唯一的名字绑在一起,外部资源可以通过名字获得某对象的引用。

在javax.naming的包包中提供Context接口,提供了两个很好用的方法:
<1> void bind( String name , Object object )
将名称绑定到对象。所有中间上下文和目标上下文(由该名称最终原子组件以外的其他所有组件指定)都必须已经存在。PS:名字不能为空~
<2>Object lookup( String name )
检索指定的对象。如果 name为空,则返回此上下文的一个新实例(该实例表示与此上下文相同的命名上下文,但其环境可以独立地进行修改,而且可以并发访问)。

外部资源访问对象工厂中的工程图:



例:
=================将以下代码段添加到server.xml中的<Host>中============ <!-- configure DataSource. Add the following code into server.xml -->

<Context path="/bookstore" docBase="bookstore" debug="0"
reloadable="true" >

<!-- 数据源名称 -->
<Resource name="jdbc/BookDB"
auth="Container"
type="javax.sql.DataSource" />

<ResourceParams name="jdbc/BookDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
--> <!-- 活动状态最大连接数 -->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
--> <!-- 空闲状态数据库连接最大数 -->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
--> <!-- 数据库处于空闲状态的最长时间 -->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<!-- MySQL dB username and password for dB connections --> <!-- 指定连接数据库的用户名及密码 -->
<parameter>
<name>username</name>
<value>dbuser</value>
</parameter>
<parameter>
<name>password</name>
<value>1234</value>
</parameter>

<!-- Class name for mm.mysql JDBC driver --> <!-- 指定JDBC驱动 -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>

<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
--> <!-- 指定连接数据库的URL -->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/BookDB?autoReconnect=true</value>
</parameter>
</ResourceParams>

</Context>=================将以下代码段添加到web.xml中============= <!-- add the following code into <webapp> in web.xml -->

<resource-ref>
<description>DB Connection</description>
//对所引用的资源的说明
<res-ref-name>jdbc/BookDB</res-ref-name>
//指定所引用资源的JNDI名字,与<Resource>元素中的name属性相对应
<res-type>javax.sql.DataSource</res-type> //指定所引用资源的类名字,与<Resource>元素中的type属性相对应
<res-auth>Container</res-auth> //指定管理所引用的资源的Manager,与<Resource>元素中的auth属性对应
</resource-ref>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: