tomcat JNDI 的配置
来源:互联网 发布:linux local 编辑:程序博客网 时间:2024/05/17 08:41
JNDI Tomcat 启动错误
Scene:
在 Tomcat 测试 JNDI。(配置context.xml和web.xml的JDBC资源参数以供web应用连接至mysql数据库)
错误信息:
Creation of the naming context failed: javax.naming.OperationNotSupportedException: Context is read only
Solution:
? No solution yet
(link:http://stackoverflow.com/questions/15340218/what-could-cause-javax-naming-operationnotsupportedexception-when-tomcat-start)
Tomcat MySQL JNDI 配置
Scene:
利用 JNDI 来为某个 App 配置数据源。(在 apache tomcat 提供的文档里面有提及"JNDI Datasource How-To")
好处是在该 App 的代码文件里面可以实用 JNDI 提供的接口来链接数据库,以后更改数据库产品的时候只需要一次性配置该 App 的配置文件就可以了。而不需要更改所有文件里面的代码。
Solution:
需要配置的文件有 context.xml 和 web.xml。当然(mysql的驱动可以放在web应用的 $root/WEB-INF/lib 下面,也可以放在 $tomcat/lib 下面)
context文件内容(其中的各项值的意义可以参考 tomcat 提供的文档):
在 context.xml 里面添加 <Context> 结点,在 <Context> 中定义 <Resource>。
- <?xml version="1.0" encoding="UTF-8"?>
- <Context>
- <Resource name="jdbc/mysql"
- auth="Container"
- type="javax.sql.DataSource"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/test"
- username="root"
- password="tsm;jd"
- removeAbandoned="true"
- removeAbandonedTimeout="120"
- maxWait="60"
- maxActive="20"
- maxIdle="10"
- />
- </Context>
web.xml文件需要添加的内容:
在 web.xml 里面定义 <resource-ref> 结点。
- <resource-ref>
- <description>the data source of mysql database, the application could use this resource to
- connect to the database</description>
- <res-ref-name>mysql-ds</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- <lookup-name>jdbc/test</lookup-name>
- </resource-ref>
可以通过菜单(安装 JBoss 社区提供的 JBoss Studio 在 eclipse 里面)
调用:
- import java.sql.Connection;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- private static Connection CONNECTION;
- static
- {
- try
- {
- Context ctx = new InitialContext();
- Object datasourceRef = ctx.lookup("java:comp/env/jdbc/mysql");
- DataSource ds = (DataSource)datasourceRef;
- CONNECTION = ds.getConnection();
- }
- catch (NamingException e)
- {
- e.printStackTrace();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
0 0
- tomcat JNDI 的配置
- tomcat下的JNDI配置
- tomcat配置全局JNDI和私有jndi的方式
- 怎样配置tomcat的jndi数据库连接?
- tomcat jndi 数据源 配置的一个例子
- spring配置tomcat的JNDI数据源
- tomcat配置jndi的三种方式
- tomcat配置jndi的三种方法
- JNDI配置Tomcat的数据库连接池
- spring配置tomcat的jndi数据源
- tomcat下jndi的全局配置方式
- tomcat下的jndi简单配置
- JNDI初探之tomcat的datasource配置
- Tomcat hibernate JNDI 配置
- Tomcat JNDI配置
- tomcat 配置JNDI
- tomcat JNDI数据源配置
- tomcat jndi配置
- Xcode7 插件制作入门
- 《C++ Primer Plus》——编程练习答案(1)
- 开发过程中遇到的BUG
- 手机号正则表达式iOS
- openwrt添加一条开机自动执行的命令
- tomcat JNDI 的配置
- Python正则表达式指南
- Chrome控制台使用详解
- 九度:斐波那契数列
- ASPX中使用EF实现增删改查
- Android安全专项-AndBug动态调试工具
- ubuntu下设置开机自启动项
- ASP.NET文件的上传
- 技术团队招聘原则