0

We have a project that have quite a lot of dependencies. I am confused in Websphere resource binding and resource definition at all now.

  1. ejb-jar.xml describes resources.
  2. persistance.xml describes database resources.
  3. We can have web.xml, where we describes resources also.
  4. We can have ibm-web-bnd.xml, where we can have binding for resources.

Lets say, I have a *.jar that has definition of data source in persistance.xml (jta-data-source), same data source in ejb-jar.xml (enterprise-beans/session/resource-ref), and also has binding for this data source ibm-web-bnd.xml (resource-ref).

Now I want to use this *.jar in my *.war.

Lets assume that I have configured all resources on my Websphere Liberty server correctly.

During application start up, I've got following error for this *.jar:

Unable to use JDBC Connection to create Statement
java.sql.SQLException: Unsupported use of GenericConnection.  A 
GenericConnection is provided during application start when creating an 
EntityManagerFactory for a persistence unit which has configured one of its 
datasource to be in the component naming context; java:comp/env. During 
application start, the component naming context will not exist, and the 
correct datasource cannot be determined. When the persistence unit is used, 
the proper datasource and connection will be obtained and used.

Questions:

  1. Why my *.jar do not see Data Source, that I have defined on my Websphere Libery (in server.xml)? Do I miss some binding in my *.war?
  2. Why do we describe resources in web.xml? I thought that this is definition of web application, like a servlet mapping, filters, etc.
  3. Why same data source is described in both persistance.xml and ejb-jar.xml (is it not enough with persistance.xml?)?
  4. Does resource binding delay somehow dependency injection?

Code in *.jar that I can't modify, but have to use in my *.war:

ejb-jar.xml

...
<enterprise-beans>
<session id="MyEntityManagerBean">
    <ejb-name>MyEntityManagerBean</ejb-name>
    <ejb-class>somepackage.MyEntityManagerBean</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Container</transaction-type>          
    <resource-ref id="some_id_goes_here">
        <res-ref-name>jdbc/my_ds</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</session>
</enterprise-beans>
...

persistance.xml

<persistence-unit name="MyPersistentUnit" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:comp/env/jdbc/my_ds</jta-data-source>     
...
</persistence-unit>

ibm-ejb-jar-bnd.xml

...
<session name="MyEntityManagerBean">
    <resource-ref name="jdbc/my_ds" binding-name="jdbc/my_ds"/>
</session>
<session name="MyEntityManagerBean2">
    <resource-ref name="jdbc/my_ds" binding-name="jdbc/my_ds"/>
</session>
...  

MyEntityManagerBean.java (also same *.jar)

    @PersistenceContext(unitName="MyPersistentUnit")
    protected EntityManager entityManager;  

And problem starts, when I add this *.jar as Maven dependency to my *.war.

Thanks in advance.

Pavel
  • 653
  • 2
  • 11
  • 31
  • A resource reference doesn't define a DataSource, it defines the need for a datasource and the name the application will use to look it up. The same for the persistence.xml. Have you defined a dataSource in the Liberty server.xml or the data-source element in the web.xml or ejb-jar.xml? – Alasdair May 04 '17 at 18:47
  • Yep, I have - in server.xml. – Pavel May 04 '17 at 19:05
  • Attach relevant parts of these xmls, as you probably are not using resource ref syntax in persistence.xml, or have some misconfiguration. Also add code fragment where you are injecting EntityManager. – Gas May 05 '17 at 13:00
  • Done, please take a look. – Pavel May 05 '17 at 14:40

0 Answers0