先说Resin的JNDI数据池连接配置(Resin版本3.1)
有两个地方可以共你设置
1, Resin 下的conf/resin.conf
里有这一部分 ,很明显告诉你在这设置:
<!--
- Sample database pool configuration
-
-
The JDBC name is
java:comp/env/jdbc/test
<database>
<jndi-name>jdbc/mysql</jndi-name>
<driver
type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/test</url>
<user></user>
<password></password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
-->
这个地方我想是设置全局JNDI的部分
2,在你的工程文件如webapps/YourJspApp/WEB-INF/web.xml里
的</web-app>前写一下部分,(说白了就是把1,中的部分写到web.xml里就可以了,方便把说明我统一用的UTF-8编码)
<database>
<jndi-name>jdbc/YourDataBase</jndi-name>
<driver
type="com.mysql.jdbc.Driver">
<url>jdbc:mysql://YourDataBaseServerIP:PORT/YourDataBase?autoReconnect=true&useUnicode=true&characterEncoding=UTF8</url>
<user>USERNAME</user>
<password>USERPASSWORD</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
就这样,配置部分就完工了.
Tomcat的配置,最让人头痛.(我使用的版本Tomcat6.0.13)
首先,大家要注意,Tomcat的不同版本Tomcat的JNDI的配置是不同的,像Tomcat5.5以前的版本和Tomcat5.5以后的版本,如Tomcat6.0配置很不一样,Tomcat5.0或以前的配置网上说的很多了,我也收录了几篇明白点的文章.(在小站文章搜索里填关键词"池"就有Tomcat其他版本配置)现在把Tomcat6.0的配置说一下:
首先,你不用配置conf/下的web.xml也不要改server.xml,因为5.5以上的版本不需要修改这两个全局配置文件,否则,您自己可以试试.关键是修改conf/context.xml工程上下文配置文件和你自己的工程文件里的WEB-INF/web.xml工程配置文件.修改如下:
Conf/context.xml
在<context></context>之间写JNDI资源配置信息
<Resource
name="jdbc/YourDataBase" type="javax.sql.DataSource"
auth="Container"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
maxWait="5000"
maxActive="4"
password="USERPASSWORD"
username="USERNAME"
url="jdbc:mysql://YourServerIP:PORT/YourDataBase?autoReconnect=true&useUnicode=true&characterEncoding=UTF8"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
/>
保存
你的工程文件下WEB-INF/web.xml里的</web-app>前
<resource-ref>
<description>MyApp
DataBase
Connection</description>
<res-ref-name>jdbc/YourDataBase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
保存OK
重起TOMCAT运行通过!可想而知TOMCAT6做了很大的改进不仅仅在内存优化上还是配置上比以前都有进步.
JNDI
JAVABEAN:
package com.jndi;
import java.sql.Connection;
import
java.sql.*;
import javax.sql.*;
import javax.sql.DataSource;
import
javax.naming.*;
import javax.naming.NamingException;
import
javax.naming.Context;
import javax.naming.InitialContext;
import
javax.naming.NamingEnumeration;
public final class JndiDB
{
public
static synchronized Connection getJndiConnection() throws
Exception
{
try{
Context initCtx = new
javax.naming.InitialContext();
Context envCtx =
(Context)initCtx.lookup("java:comp/env");//似乎java:/comp/env也可以大家不妨一试
DataSource
ds = (DataSource)envCtx.lookup("jdbc/YourDataBase");
// DataSource ds =
(DataSource)intCtx.lookup("java:comp/env/jdbc/YourDataBase");
return
ds.getConnection();
}catch(Exception e){
throw e;
}
}//END
getJndiConnection();
}//End
Class
官方参考:
Tomcat6.0
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
部分官方资料如下:
Context
configuration
Configure the JNDI DataSource in Tomcat by adding a declaration
for your resource to your Context.
For example:
<Context
path="/DBTest" docBase="DBTest"
debug="5" reloadable="true"
crossContext="true">
<!-- maxActive: 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.
-->
<!-- maxIdle: Maximum number of idle dB connections to
retain in pool.
Set to -1 for no limit. See also the DBCP documentation on
this
and the minEvictableIdleTimeMillis configuration
parameter.
-->
<!-- maxWait: 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.
-->
<!-- username and password: MySQL dB username and
password for dB connections -->
<!-- driverClassName: Class name for
the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using
Connector/J though.
Class name for the official MySQL Connector/J driver is
com.mysql.jdbc.Driver.
-->
<!-- url: 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.
-->
<Resource name="jdbc/TestDB" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30"
maxWait="10000"
username="javauser" password="javadude"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
</Context>
3. web.xml configuration
Now create a WEB-INF/web.xml for this test
application.
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>MySQL
Test App</description>
<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>
</web-app>
4. Test code
Now create a simple test.jsp page for use later.
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"
%>
<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo,
bar from
testdata
</sql:query>
<html>
<head>
<title>DB
Test</title>
</head>
<body>
<h2>Results</h2>
<c:forEach
var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar
${row.bar}<br/>
</c:forEach>
</body>
</html>
That JSP page makes use of JSTL‘s SQL and Core taglibs. You can get it from
Sun‘s Java Web Services Developer Pack or Jakarta Taglib Standard 1.1 project -
just make sure you get a 1.1.x release. Once you have JSTL, copy jstl.jar and
standard.jar to your web app‘s WEB-INF/lib directory.
Finally deploy your
web app into $CATALINA_HOME/webapps either as a warfile called DBTest.war or
into a sub-directory called DBTest
Once deployed, point a browser at
http://localhost:8080/DBTest/test.jsp to view the fruits of your hard
work.
Oracle 8i, 9i & 10g
0. Introduction
Oracle requires minimal
changes from the MySQL configuration except for the usual gotchas :-)
Drivers
for older Oracle versions may be distributed as *.zip files rather than *.jar
files. Tomcat will only use *.jar files installed in $CATALINA_HOME/lib.
Therefore classes111.zip or classes12.zip will need to be renamed with a .jar
extension. Since jarfiles are zipfiles, there is no need to unzip and jar these
files - a simple rename will suffice.
For Oracle 9i onwards you should use
oracle.jdbc.OracleDriver rather than oracle.jdbc.driver.OracleDriver as Oracle
have stated that oracle.jdbc.driver.OracleDriver is deprecated and support for
this driver class will be discontinued in the next major release.
1. Context
configuration
In a similar manner to the mysql config above, you will need to
define your Datasource in your Context. Here we define a Datasource called
myoracle using the thin driver to connect as user scott, password tiger to the
sid called mysid. (Note: with the thin driver this sid is not the same as the
tnsname). The schema used will be the default schema for the user scott.
Use
of the OCI driver should simply involve a changing thin to oci in the URL
string.
<Resource name="jdbc/myoracle"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
username="scott"
password="tiger" maxActive="20" maxIdle="10"
maxWait="-1"/>
2. web.xml
configuration
You should ensure that you respect the element ordering defined
by the DTD when you create you applications web.xml
file.
<resource-ref>
<description>Oracle Datasource
example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3. Code example
You can use the same example application as above
(asuming you create the required DB instance, tables etc.) replacing the
Datasource code with something like
Context initContext = new
InitialContext();
Context envContext =
(Context)initContext.lookup("java:/comp/env");
DataSource ds =
(DataSource)envContext.lookup("jdbc/myoracle");
Connection conn =
ds.getConnection();
//etc.
发表评论
-
数据库常识
2011-07-05 14:56 0delete from aa truncate table ... -
Oracle存储过程实例
2010-01-28 22:47 9204create or replace proc ... -
JDBC事务
2010-01-28 22:28 0作者:Jack Shirazi 开发 ... -
Oracle与SQL Server事务处理的比较
2010-01-28 22:03 1474事务处理是所有大型数 ... -
Sqlldr简介
2009-10-21 14:10 1246罗列了网上常见的三篇Sqlldr的介绍 一:sql lo ... -
Oracle中实现行列转换的方法
2009-07-21 22:31 1209student subject grade -------- ... -
oracle 中日期的加减
2009-07-21 22:00 21496加法 select sysdate,add_mo ... -
Oracle 分页
2009-03-26 17:07 0select * from (select rownum ... -
MYSQL的乱码问题
2009-02-17 12:45 800总结了一下几个处理方法 1:改变数据库的默认编码配置,在MYS ... -
几种开源数据库连接池的使用 --转载
2009-01-17 13:36 1763转载: http://www.blogjava.net/fa ... -
tomcat5.0与tomcat5.5的数据库连接池jndi配置区别
2009-01-17 13:35 1712在tomcat5.5版本以前,可以说jndi配置相对是比较复杂 ... -
tomcat5.0连接池配置成功了,池连,JNDI
2009-01-17 13:34 0http://heisetoufa.iteye.com/blo ... -
java连接oracle数据库的各种方法及java在数据库中的含义
2009-01-17 13:34 1214java与oracle的接口: 在数据库中运行JA ... -
开源数据库连接池proxool
2009-01-17 13:32 1693关键词:proxool 连接池 开源 可以根据自己的实际情况, ... -
java实现 excel 中数据导入 oracle
2009-01-17 13:31 2912ORACLE是有一个叫ADI的解决方案 所需的额外包:comm ... -
registerOutParameter Method (SQLServerCallableStat
2009-01-17 13:31 1061registerOutParameter Method (SQ ... -
JDBCTM 指南CallableStatement
2009-01-17 13:31 10507 - CallableStatement 本概述 ... -
OJB查询
2009-01-17 13:27 1514该文档介绍了不同查询 ... -
OJB MySQL 配置
2009-01-17 13:26 10901. repository-database.xml < ... -
深入解析什么是存储过程
2009-01-17 13:26 2209深入解析什么是存储过 ...
相关推荐
自己总结的三种使用方式,对比来看选择最适合你的方式
Tomcat jndi 配置数据源过程 Tomcat jndi 配置数据源过程 Tomcat jndi 配置数据源过程 Tomcat jndi 配置数据源过程
tomcat上配置的JNDI数据源的几种方式
帆软报表FineReport中数据连接中的Tomcat配置JNDI连接,要通过JNDI方式定义数据连接,首先在Tomcat服务器配置好JNDI,然后在设计器中直接调用JNDI的名字,即可成功使用JNDI连接,本文进行详细演示。
TOMCAT8 JNDI对用户名和密码加密
讲述tomcat5.0和tomcat5.5.12配置连接池(JNDI)的不同配置方法
自定义jndi数据源factory类,用于解密jndi中的加密数据,解密方式为DES,具体可根据实际需求修改。
Tomcat JNDI 数据源 自动生成程序 Servlet 版 本程序自动判断Tomcat版本,兼容tomcat 5.0、5.5、6.0等版本, 根据项目名自动生成 JNDI 配置文件,使程序员从手动配置JNDI数据源中解脱出来。 使用说明: 1、把...
tomcat配置jndi几个方式
Jndi数据连接池 内有详细说明 包各位看了 就可以很轻松的写出自己的 Jndi连接项目
Tomcat中配置jndi数据源连接池(自己写的) 做过实用 没有问题
TOMCAT JNDI配置
在Tomcat配置JNDI数据源的三种方式
NULL 博文链接:https://buliedian.iteye.com/blog/858026
hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+...
Tomcat配置JNDI数据源的N种方法
Tomcat JNDI配置说明
数据挖掘技术因“数据爆炸但知识贫乏”而应运而生,并因...本课题就是以此作为指导思想,对选课系统中的数据进行采集与分析,提出了结合数据挖掘和C/S+B/S架构技术的设计思路,开发出适合学分制和决策分析的选课系统。
Tomcat6+spring+jndi配置数据源说明.docx
简单我tomcat5.0中的JNDI应用