由于项目中多个小应用(类似插件)需要使用数据库连接池,不能为每一个应用中配置连接池。故采用JNDI的模式进行配置。

      我们使用的web服务器有tomcat和jboss两种。分别介绍其在这两种服务器下并且在两种应用模式spring+hebernat与quartz下的配置。

tomcat下:

1)在tomcat 6.0下配置JNDI及在Spring中的使用 (引用1)已经详细介绍了其往spring应用中添加的详细过程。(引用3)

2)quartz中添加tomcat jndi datasource很简单,只需要调用org.quartz.dataSource.myDS.jndiURL设置即可。

3)tomcat下quartz中出现 not bound in context的问题是要写完整的jndi名字。必须加上“java:comp/env/XXX“,查看intialcontext源代码可知,与spring中名字格式保持一致即可。markmail网站上可以搜到很多这种问题的解决方案(引用2)。

4)(这一点我也不知道为什么。)有时我的jndi名字就是不能绑定下来,但是当我把名字尽量缩短就不会出现问题。测试许多次都出现不知道何原因。我的命名是”jndi/abcd“。

5)我尝试将我的tomcat6.0添加admin模块(引用5),并使用其添加jndi服务,但是不成功。未曾找到admin配置jndi修改的文件,明显不是引用1,3中的config目录下的context文件。

jboss下:

1)jboss下部署配置spring jndi(引用4)详细介绍jboss下的spring配置。

2)quartz类似tomcat。

3)我曾经尝试在spring中配置使用跨jvm的jndi的datasource,进行远程调用。

注意使用<use-java-context>false</use-java-context>可以进行跨jvm调用(引用6)。但是在spring直接进行bean配置会出现问题javax.naming.Resource  javax.sql.DataSource typemismatch。需要手写一个bean进行这种转换(强制转换),可以参考引用6中的例子。(引用7)但是,这里有一点提醒:Using Datasources from remote is not recommended and might lead into trouble easily because the app server looses control over the connections. Be warned!Once the maximum connection in the pool are reached no other connections can be obtained. For the reason that the server does not manage the connections anymore, he is not aware that you close the connection.考虑到实际情况,最终我放弃了跨JVM的jndi的调用。

--------------------------------------------

现在JNDI工作正常,要进行观察,适当调整参数,优化服务器性能。

--------------------------------------------------

引用1:在tomcat 6.0下配置JNDI及在Spring中的使用 http://karidyang.javaeye.com/blog/216125

引用2:markmail网站

http://markmail.org/search/?q=quartz+tomcat+jndi

引用3:tomcat+jndi+spring配置

http://apps.hi.baidu.com/share/detail/31393331

引用4:jboss下部署配置spring jndi

http://www.blogjava.net/sxyx2008/archive/2010/09/08/331382.html

引用5:apache-tomcat-6.0.18下配置Tomcat Administration Web Application Tool

http://hi.baidu.com/%B9%C2%B6%C0%5F%CE%E8%D5%DF/blog/item/33c11703785902713812bb49.html

引用6:Access JBoss DataSource Remotely from Java Applications

http://javahowto.blogspot.com/2006/08/access-jboss-datasource-remotely-from.html

引用7:Using JBoss DataSource from standalone client

http://forum.springsource.org/showthread.php?16076-Using-JBoss-DataSource-from-standalone-client