You are getting that exception because the default transaction types of
a PersistenceUnit are different between SE and EE environment. So, you
can blame it on JPA spec. This is what the spec says about
transaction-type attribute in persistence.xml:
/In a Java EE environment, if this element is not specified, the default
is JTA. In a Java SE environment, if this element is not speci-
fied, a default of RESOURCE_LOCAL may be assumed.
/
The spec also says how data-source is defaulted:
/In Java EE environments, the jta-data-source and non-jta-data-source
elements are used
to specify the global JNDI name of the JTA and/or non-JTA data source to
be used by the persistence
provider. If neither is specified, the deployer must specify a JTA data
source at deployment or a JTA
data source must be provided by the container, and a JTA
EntityManagerFactory will be created to cor-
respond to it.
/
When you switched to EE environment, the default became JTA, but you
have not configured any jta-data-source, so container used a default
data-source for you which in GlassFish is a Derby based DataSource.
That's why it tried to open a connection to Derby. You really need to
configure a JTA DataSource and specify that value in <jta-data-source>
in persistence.xml.
Thanks,
Sahoo
Comerford, Sean wrote:
> So I知 stumped by a strange exception I知 seeing and hoping maybe
> someone has seen something similar.
>
> My app + config works fine in my standalone dev instance but my prod
> clustered instance is not (though I知 not sure it痴 at all related to
> clustering).
>
> I have an EJB getting an EMF injected:
>
> @PersistenceUnit(unitName="NbaJpa")
> private EntityManagerFactory entityManagerFactory;
>
> That persistence unit is in turn configured to a the JDBC resource:
>
> <persistence-unit name="NbaJpa" transaction-type="RESOURCE_LOCAL">
> <provider>org.hibernate.ejb.HibernatePersistence</provider>
> <non-jta-data-source>jdbc/SportwareNBA</non-jta-data-source>
>
> That JDBC resource is properly configured and targeted + enabled on
> the cluster instance I知 running on. If I do the Ping test from the
> admin console, it succeeds.
>
> But at runtime, when I access the EJB using JPA, I知 getting a
> connection refused exception and it seems to be trying to access the
> local derby DB on port 1527 (see last part of stack trace below).
>
> Any ideas what to look at? Why would it be trying to access derby
> instead of the jdbc resource specified in my persistence.xml?
>
> Caused by: javax.persistence.PersistenceException:
> org.hibernate.exception.GenericJDBCException: Cannot open connection
> at
> org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
> at
> org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:202)
> at com.espn.sports.dao.JpaDAOImpl.findById(JpaDAOImpl.java:273)
> at
> com.espn.sports.ws.basketball.BasketballTeamService.getTeam(BasketballTeamService.java:30)
> at
> com.espn.sports.ws.basketball.BasketballService.getTeam(BasketballService.java:155)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1011)
> at
> com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:175)
> at
> com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2920)
> at
> com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4011)
> at
> com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:203)
> ... 17 more
> Caused by: org.hibernate.exception.GenericJDBCException: Cannot open
> connection
> at
> org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
> at
> org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
> at
> org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
> at
> org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
> at
> org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
> at
> org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
> at
> org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
> at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
> at org.hibernate.loader.Loader.doQuery(Loader.java:696)
> at
> org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
> at org.hibernate.loader.Loader.loadEntity(Loader.java:1885)
> at
> org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
> at
> org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
> at
> org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3062)
> at
> org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
> at
> org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
> at
> org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
> at
> org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
> at
> org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
> at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)
> at org.hibernate.impl.SessionImpl.get(SessionImpl.java:843)
> at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
> at
> org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
> ... 29 more
> 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: connect.
> 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)
> ... 47 more
>
> --
> Sean Comerford, Software Engineer
> ESPN.com Site Architecture Group
> Office: 860.766.6454 Cell: 860.329.5842