Hi Sean,
It is hard to tell what is going on without some sort of reproducible
example.
Jersey will attempt to look up the EntityManagerFactory using JNDI. In
this case the JNDI name will be "java:comp/env/punit-ref", where
"punit-ref" is declared in the web.xml:
<init-param>
<param-name>unit:NbaJpa</param-name>
<param-value>punit-ref</param-value>
</init-param>
<persistence-unit-ref>
<persistence-unit-ref-name>punit-ref</persistence-unit-ref-
name>
<persistence-unit-name>NbaJpa</persistence-unit-name>
</persistence-unit-ref>
I presume a non-null value is getting injected otherwise you would get
a NPE.
Note that when you say CMP do you really mean Container Managed
Persistence? In this case since you are using EntityManagerFactory you
need to managed the transactions yourself and they are not managed by
the container.
Also note that the use of the ServletAdapter is kind of an interim
solution until Java EE 6 is released. Pavel recently updated the
bookmark sample and also created a new one, bookmark-em, that uses the
EE 6 feature of managed beans.
If you can send an example of your set up perhaps Pavel may be able to
spot what is wrong?
Paul.
On Dec 1, 2009, at 10:57 PM, Comerford, Sean wrote:
> I have a JPA (w/ Hibernate) connection to a SQL server DB
> configured. I have a bunch of Jersey resources configured and
> successfully working by getting an EntityManagerFactory directly via
> a call to Persistence.createEntityManagerFactory(“foo”).
>
> Now I’m trying to convert these resources to have Glassfish inject
> the EMF as is done in the bookmark Jersey sample... i.e:
>
> @PersistenceUnit(unitName = "NbaJpa")
> EntityManagerFactory entityManagerFactory = null;
>
> I have changed my web.xml to use the Jersey ServletAdaptor (as shown
> in the bookmark example) and changed the params as appropriate for
> data source.
>
> I’m guessing I’m missing something completely brain dead but as far
> as I can tell, the only difference at this point with my project and
> the bookmark example is I’m NOT using a JTA data source. But when I
> try to access the Jersey resource that uses CMP I get the exception
> shown below. For what it’s worth, my other resources that still call
> Persistence.createEMF to get the EMF directly work fine.
>
> Any ideas what I’ve missed? It seems to be trying to connect to
> localhost 1527 (the derby port I believe) for unknown reasons. My
> one and only persistence unit is configured as a JDBC resource to
> talk to a remote host on port 1433 (the SQL server port).
>
> javax.persistence.PersistenceException:
> org.hibernate.exception.GenericJDBCException: Cannot open connection
> ... Blah blah blah ....
> Caused by: java.sql.SQLException: Error in allocating a connection.
> Cause: Connection could not be allocated because:
> java.net.ConnectException : Error connecting to server localhost on
> port 1527 with message Connection refused.
> at com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:
> 115)
> at
> org
> .hibernate
> .ejb
> .connection
> .InjectedDataSourceConnectionProvider
> .getConnection(InjectedDataSourceConnectionProvider.java:46)
> at
> org
> .hibernate
> .jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
> ... 65 more
>
>
> --
> Sean Comerford, Software Engineer
> ESPN.com Site Architecture Group
> Office: 860.766.6454 Cell: 860.329.5842