2

I am trying to follow this tutorial and at same time adapting it to run in on Websphere 8.5.5 with default Websphere JPA ( OpenJPA ) implementation.

I created the project in Eclipse and use WAS for Developers.

Initially, when running the application, I was receiving errors about resource-ref not available ( do not remember the exact error message )

After some research and a lot of trial-and-error approach, I got the application to work. I had to make ( or made ) changes in the web.xml and ibm-web-bnd.xml to add resource-ref to the datasource defined in persistence.xml ( don't remember, if I changed anything else in the trial and error attempt ).

But now, I have the following questions

  • What are the changes that are actually needed to be made to make the application work?

  • What is the relationship between datasource in persistence.xml and resource-ref in web.xml / ibm-web-bnd.xml?

  • This is currently done in a dynamic web project. What other changes do I need to make, if I want to create a JPA in an EJB project or a JPA project?

  • In this tutorial, the JPA is in dynamic web project. What changes do I need to make, if I create an ejb project / JPA project and want to execute the application that way, instead of defining the JPA in web project?

  • Is there any technote or other documentation that explains this?

adbdkb
  • 1,897
  • 6
  • 37
  • 66

2 Answers2

1

I would suggest to use the Websphere Liberty Profile, 8.5.5.6 (current version).

Some clarifications (that you already might know):

web.xml contains your servlets, mappings, init params, listeners, security, datasource, etc.

persistence.xml is required when dealing with persistence unit. It defines the configuration (JPA access intents, isolation level, lock levels, etc). It contains the entity classes and the applied data source.

ibm-web-bnd.xml it contains the mappings of the logical resources. Datasource, Jms bindings, Ejb bindings...

I would recommend IBM redbooks, e.g. WebSphere Application Server V8.5 Administration and Configuration Guide for the Full Profile

Mapping between resource reference from web.xml and JNDI name from Application server can be stored in 2 places. You can define this mapping during development in the 'binding file' - in case of the web module it is stored in WEB-INF/ibm-web-bnd.xml file. If you use RAD, you can create binding file via right click on web project JEE -> generate Bindings deployment descriptor. And then in the descriptor you put something like this: Then it is used during installation. If you didnt specify bindig via file, you can do this during application installation eg. via console. Then binding file is created and it is stored in: PROFILE_ROOT\installedApps\cellName\Application.ear\WebModule.war\WEB-INF\ibm-web-bnd.xml and in PROFILE_ROOT\config\cells\cellName\applications\Application.ear\deployments\Application\WebModule.war\WEB-INF\ibm-web-bnd.xml

developerworks forum

MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
  • Thanks. My question is more on the lines of defining resource-ref in web.xml and ibm-web-bnd.xml for the datasource mapping between persistence.xml defined jta-datasource and resource-ref in these two files and whether the definitions MUST exist in all the three xml files for JPA to work in a dynamic web application? – adbdkb Jul 26 '15 at 01:50
  • I m pretty sure that you dont need the datasource in web.xml, the lookup will be done directly against the app server. I had a dynamic web application set up just 3 days ago. Without the datasource in web.xml and it worked fine. The datasource was setup for the WLP configuration. The binding file is important if you have not set it up in the admin console, so that part can actually also be left out. The only one that needs to be setup in your case is for the persistence.xml – MrSimpleMind Jul 26 '15 at 20:04
1

I'll try to clarify some of your questions.

Datasource is usually defined and configured in the server configuration files. It can be defined in application (via web.xml or annotations), but I'd avoid that since it's less flexible and hardcodes database details in the application itself.

Datasource is made available to the application via JNDI name, let's say jdbc/myDS.

In the persistence.xml you can provide datasource JNDI name that will be used by your JPA application. The JNDI name can be either in the global name (in that case it must match the JNDI name defined in the server - so in our case jdbc/myDS) or as resource reference name (e.g. java:comp/env/jdbc/myDSReference).

If you use resource reference, it provides you with better flexibility as it dont have to match JNDI name defined in the server, however you will have to define that reference in your application and provide binding for it.

There are several ways to define reference and binding:

  • Annotations
    Use @Resource annotation in a servlet to define reference name and binding via lookup attribute:

    @Resource(type=DataSource.class, name="jdbc/myDSReference", lookup="jdbc/myDS")
    
  • Deployment descriptor
    Use web.xml to define resource reference via <resource-ref>

The ibm-mmm-bnd.xml file (where mmm can be web in case of web module, or ejb-jar in case of ejb module) is a binding file, which can provide mapping between your references and global JNDI names defined on the server. You can use it instead of the lookup attribute (you have to use it in pre Java EE 6 applications, since there was no lookup attribute then).

That binding can also be defined and changed via WebSphere web administrative console during or after application installation or via wsadmin scripting.

In case of EJB project - if you want to use references, you will have to defined them as a resource reference for any given bean which will access the EntityManager. Again either via annotation or deployment descriptor.

Gas
  • 17,601
  • 4
  • 46
  • 93