开发者

Custom resource in JNDI on different application servers

开发者 https://www.devze.com 2023-01-17 06:50 出处:网络
Preface: Most of J2EE applications are using container managed datasources through JNDI. This is fine as it gives one place for configuring these connections.

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 开发者_Python百科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


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.


If you are using Hibernate, this is the property to add in persistence unit :

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

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 :

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

Also editable in JBoss 7 Admin.

On Weblogic, you may inject this in the Connection Pools.

On Websphere, this should be similar.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消