不同应用程序服务器上 JNDI 中的自定义资源
前言:
大多数 J2EE 应用程序都通过 JNDI 使用容器管理的数据源。这很好,因为它为配置这些连接提供了一个位置。
当我们想要使用 ORM 框架(如 hibernate)或必须知道默认模式(主要是 Oracle,也可能是其他模式)的东西时,就会出现问题,这可能与用于连接数据库的用户名不同。
我想将默认架构名称放在靠近数据源定义的位置。其中一种选择是将其放入 JNDI 中。然后,我将在构建 EntityManager 之前从那里手动读取(实际上使用 Spring)。
我发现有一种简单的方法可以在 Apache Tomcat 中指定自定义资源(在这种情况下,它将是具有默认模式名称的字符串),如下所示(如果我错了,请纠正我):
<Environment name="schemaNames/EmployeeDB"
type="java.lang.String"
value="empl"
description="Schema name of Employees Database for HR Applications"/>
无论如何,考虑到这可以在 Apache 中完成Tomcat,我应该如何在其他应用程序服务器中配置相同的自定义 JNDI 资源(字符串类型):
- JBoss 4/5
- WebSphere 6/7
- WebLogic 9/10
如果您了解其他服务器,那就太好了。
另外,作为替代方案,我不想将架构名称放入系统属性或环境变量中。
非常感谢 !
更新:
在JBoss上找到了一些实现它的方法。我没有测试过。
http://forums.java.net/jive/thread.jspa?messageID= 316228
找到了 WebLogic 的信息,但他们谈论以编程方式而不是通过配置来实现:
http://weblogic -wonders.com/weblogic/2010/06/12/binding-objects-in-weblogic-servers-jndi-tree/
http://forums.oracle.com/forums/thread.jspa?messageID= 4397353
Preface:
Most of J2EE applications are using container managed datasources through JNDI. This is fine as it gives one place for configuring these connections.
The problem arises when we want to use ORM framework (like hibernate) or something that have to know the default schema (mostly for Oracle, may be others too), which can be different from the username that is used to connect to the DB.
I want to put the default schema name somewhere close to the datasource definition. One of the options would be to put it in JNDI. I will then manually read of from there before construction the EntityManager (well actually using Spring).
As I found out there is a simple way to specify custom resource (in this situation it will be String with default schema name) in Apache Tomcat like this (correct me if I'm wrong):
<Environment name="schemaNames/EmployeeDB"
type="java.lang.String"
value="empl"
description="Schema name of Employees Database for HR Applications"/>
Anyway, considering this can be done in Apache Tomcat, how should I configure the same custom JNDI resource (of String type) within other application servers:
- JBoss 4/5
- WebSphere 6/7
- WebLogic 9/10
If you know about other servers that would be great too.
Also, as an alternative I don't want to put the schema name in system properties or environment variables.
Thank you very much !
Update:
Found some way of achieving it on JBoss. I didn't test it tho.
http://forums.java.net/jive/thread.jspa?messageID=316228
Found information for WebLogic, but they talk about doing it programmaticly and not with configuration:
http://weblogic-wonders.com/weblogic/2010/06/12/binding-objects-in-weblogic-servers-jndi-tree/
http://forums.oracle.com/forums/thread.jspa?messageID=4397353
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于WebSphere,您实际上可以在定义的数据源中设置默认模式。它是一个名为 currentSchema 的自定义属性。 (即,在 V7 中,它是“资源”>“JDBC”>“数据源”>“您的数据源名称”>“自定义属性”>“currentSchema”。
否则,您可以使用名称空间绑定并在那里定义它:(即,在 V7 中,它是“环境” > 命名 > 名称空间绑定 如果您不想在 WebSphere 中以编程方式设置它,则可以使用 JNDI 来查找它,
因为我没有使用过它们。
For WebSphere you can actually set the default schema in your defined DataSource. It is a custom property called currentSchema. (ie, in V7 it is Resources > JDBC > Data sources > your data source name > Custom properties > currentSchema.
Otherwise you can use a Name Space Binding and define it there: (ie, in V7 it is Environment > Naming > Name Space Bindings. You can use JNDI to look this up if you don't want to programmatically set it in WebSphere.
Can't speak to JBoss and WebLogic as I haven't worked with them.
如果您使用 Hibernate,这是要在持久性单元中添加的属性:
这是 JPA 将为表名称插入的前缀。
如果您需要“更接近”AS 数据源定义的内容,您可以在数据库连接时注入一些特定于数据库的 SQL;例如 Oracle,
ALTER SESSION SET CURRENT_SCHEMA =
在 JBoss 上,您可以将其添加到数据源定义中:
也可在 JBoss 7 Admin 中编辑。
在 Weblogic 上,您可以将其注入连接池中。
在 Websphere 上,这应该是类似的。
If you are using Hibernate, this is the property to add in persistence unit :
That is the prefix that JPA will insert for table names.
If you need something 'closer' to the AS Datasources definitions, you may inject some DB-specific SQL at DB connection time; for instance Oracle,
ALTER SESSION SET CURRENT_SCHEMA =
On JBoss, you may add this in the datasource definition :
Also editable in JBoss 7 Admin.
On Weblogic, you may inject this in the Connection Pools.
On Websphere, this should be similar.
在 JBoss 上,您可以使用特殊的 MBean(org.jboss.naming.JNDIBindingServiceMgr) 和 service.xml 来配置 JNDI 条目,然后将这些条目映射到您的 web 应用程序中。这里对这个相当重要的过程有一个冗长的解释:
http://usna86-techbits.blogspot.com/2011/01/jboss-jndi-and-javacompenv.html
我仍在寻找一种将整个属性文件/资源包放入jndi的方法,因为当您想要将大量属性放入 jndi 并使其可供 Web 应用程序使用时,这种手动映射会变得非常乏味。
On JBoss, you can use a special MBean(org.jboss.naming.JNDIBindingServiceMgr) and a service.xml to configure JNDI-entries, and then map these entries into your webapps. There is a lengthy explication for this rather non-trivial process here:
http://usna86-techbits.blogspot.com/2011/01/jboss-jndi-and-javacompenv.html
I'm still looking for a a way to place an entire properties-file/resourcebundle into jndi, as this manual mapping gets very tedious when you have a lot of properties that you want to put into jndi and make available for your webapps.
对于 WebLogic,特别是 10.3.5 (11g),同样的问题已经困扰了相当长一段时间。
我花了一天的大部分时间环顾四周,发现的只是:http://code .google.com/p/weblogic-jndi-startup/。它工作得很好。它有一点限制:它要求您要添加到 JNDI 的对象具有带有单个 String 参数的构造函数。
对于我所需要的,weblogic-jndi-startup 不起作用,所以我在 Roger 的代码基础上构建并提出了这个: https://bitbucket.org/phillip_green_idmworks/weblogic-jndi-custom-resource-configuration/。我在 http:// /coder-in-training.blogspot.com/2012/03/weblogic-jndi-custom-resource.html
This same problem has been bothering be for quite a while for WebLogic, in particular 10.3.5 (11g).
I spent most of a day looking around and all I found was this: http://code.google.com/p/weblogic-jndi-startup/. It works just fine. It is a little restrictive: it requires the object you want to add to JNDI to have a constructor with a single String parameter.
For what I needed, weblogic-jndi-startup didn't work, so I built on Roger's code and came up with this: https://bitbucket.org/phillip_green_idmworks/weblogic-jndi-custom-resource-configuration/. I have a write up for it at http://coder-in-training.blogspot.com/2012/03/weblogic-jndi-custom-resource.html