简体   繁体   English

JNDI中不同应用程序服务器上的自定义资源

[英]Custom resource in JNDI on different application servers

Preface: 前言:
Most of J2EE applications are using container managed datasources through JNDI. 大多数J2EE应用程序都通过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. 当我们想要使用ORM框架(如hibernate)或者必须知道默认模式的东西(主要用于Oracle,也可能是其他模式)时,问题就出现了,这可能与用于连接数据库的用户名不同。

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. 其中一个选择是将它放在JNDI中。 I will then manually read of from there before construction the EntityManager (well actually using Spring). 然后我将在构建EntityManager之前从那里手动读取(实际上使用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): 正如我发现有一种简单的方法来指定Apache Tomcat中的自定义资源(在这种情况下它将是具有默认模式名称的String)(如果我错了,请纠正我):

<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: 无论如何,考虑到这可以在Apache Tomcat中完成,我应该如何在其他应用程序服务器中配置相同的自定义JNDI资源(String类型):

  • JBoss 4/5 JBoss 4/5
  • WebSphere 6/7 WebSphere 6/7
  • WebLogic 9/10 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. 找到了一些在JBoss上实现它的方法。 I didn't test it tho. 我没有测试它。
http://forums.java.net/jive/thread.jspa?messageID=316228 http://forums.java.net/jive/thread.jspa?messageID=316228

Found information for WebLogic, but they talk about doing it programmaticly and not with configuration: 找到WebLogic的信息,但是他们讨论的是以编程方式而不是配置方式:
http://weblogic-wonders.com/weblogic/2010/06/12/binding-objects-in-weblogic-servers-jndi-tree/ http://weblogic-wonders.com/weblogic/2010/06/12/binding-objects-in-weblogic-servers-jndi-tree/
http://forums.oracle.com/forums/thread.jspa?messageID=4397353 http://forums.oracle.com/forums/thread.jspa?messageID=4397353

For WebSphere you can actually set the default schema in your defined DataSource. 对于WebSphere,您实际上可以在定义的DataSource中设置默认架构。 It is a custom property called currentSchema. 它是一个名为currentSchema的自定义属性。 (ie, in V7 it is Resources > JDBC > Data sources > your data source name > Custom properties > currentSchema. (即,在V7中,它是资源> JDBC>数据源>您的数据源名称>自定义属性> 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. 否则,您可以使用名称空间绑定并在那里定义:(即,在V7中,它是环境>命名>名称空间绑定。如果您不想在WebSphere中以编程方式设置它,可以使用JNDI查找它。

Can't speak to JBoss and WebLogic as I haven't worked with them. 不能和JBoss和WebLogic说话,因为我没有使用它们。

If you are using Hibernate, this is the property to add in persistence unit : 如果您正在使用Hibernate,那么这是在持久性单元中添加的属性:

<property name="hibernate.default_schema" value="myschema" />

That is the prefix that JPA will insert for table names. 这是JPA将为表名插入的前缀。

If you need something 'closer' to the AS Datasources definitions, you may inject some DB-specific SQL at DB connection time; 如果您需要“更接近”AS数据源定义的东西,您可以在数据库连接时注入一些特定于数据库的SQL; for instance Oracle, 例如Oracle,

ALTER SESSION SET CURRENT_SCHEMA = ALTER SESSION SET CURRENT_SCHEMA =

On JBoss, you may add this in the datasource definition : 在JBoss上,您可以在数据源定义中添加它:

<new-connection-sql>
ALTER SESSION SET CURRENT_SCHEMA=myschema
</new-connection-sql>

Also editable in JBoss 7 Admin. 也可以在JBoss 7 Admin中编辑。

On Weblogic, you may inject this in the Connection Pools. 在Weblogic上,您可以在连接池中注入它。

On Websphere, this should be similar. 在Websphere上,这应该是类似的。

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. 在JBoss上,您可以使用特殊的MBean(org.jboss.naming.JNDIBindingServiceMgr)和service.xml来配置JNDI条目,然后将这些条目映射到您的Web应用程序中。 There is a lengthy explication for this rather non-trivial process here: 对于这个相当重要的过程,有一个冗长的解释:

http://usna86-techbits.blogspot.com/2011/01/jboss-jndi-and-javacompenv.html http://usna86-techbits.blogspot.com/2011/01/jboss-jndi-and-javacompenv.html

I'm still looking for aa 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. 我仍然在寻找一种方法将整个properties-file / resourcebundle放入jndi,因为当你有很多属性需要放入jndi并为你的webapps提供时,这种手动映射变得非常繁琐。

This same problem has been bothering be for quite a while for WebLogic, in particular 10.3.5 (11g). 同样的问题一直困扰着WebLogic,特别是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/ . 我花了大约一天的时间环顾四周,我找到的就是: 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. 它有点限制:它要求您要添加到JNDI的对象具有带有单个String参数的构造函数。

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/ . 对于我所需要的,weblogic-jndi-startup不起作用,所以我建立在Roger的代码上并提出了这个: 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 我在http://coder-in-training.blogspot.com/2012/03/weblogic-jndi-custom-resource.html上写了一篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM