我对Glassfish的JTA有疑问。几天我试图解决这个问题。我想在数据库中编写实体。为了简单起见,我使用了GlassfishDB。在Glassfish-Admin控制台中,我使用
jdbc / __ default
作为资源。 Ping-Connection-Pool正在运行。在persistence.xml中,我已声明Iam使用JTA,而jdbc / __ default是我的资源。如果我尝试从字段创建表,我会得到一个例外:
[EL Info]: EclipseLink, version: Eclipse Persistence Services - 2.0.1.v20100213-r6600
java.lang.NullPointerException
at `enter code here`com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:569)
at javax.naming.InitialContext.lookup(InitialContext.java:415)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:110)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:94)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:584)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:369)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:85)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:76)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:63)
[EL Severe]: Local Exception Stack:
Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.0.1.v20100213-r6600):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__default].
Internal Exception: javax.naming.NamingException: Lookup failed for 'jdbc/__default' in
SerialContext [Root exception is javax.naming.NamingException: Unable to acquire
SerialContextProvider for SerialContext [Root exception is
java.lang.NullPointerException]]
at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:451)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:116)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:94)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:584)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:369)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:85)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:76)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:63)
Caused by: javax.naming.NamingException: Lookup failed for 'jdbc/__default' in
SerialContext [Root exception is javax.naming.NamingException: Unable to acquire
SerialContextProvider for SerialContext [Root exception is
java.lang.NullPointerException]]
persistence.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Test" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
</persistence-unit>
</persistence>
Glassfish的domain.xml的<resources>
部分是:
-<resources>
<jdbc-resource object-type="system-admin" jndi-name="jdbc/__TimerPool" pool-
name="__TimerPool"/>
<jdbc-resource jndi-name="jdbc/__default" pool-name="DerbyPool"/>
-<jdbc-connection-pool name="__TimerPool" res-type="javax.sql.XADataSource"
datasource-classname="org.apache.derby.jdbc.EmbeddedXADataSource">
<property name="databaseName"
value="${com.sun.aas.instanceRoot}/lib/databases/ejbtimer"/>
<property name="connectionAttributes" value=";create=true"/>
</jdbc-connection-pool>
-<jdbc-connection-pool name="DerbyPool" res-type="javax.sql.DataSource" datasource-
classname="org.apache.derby.jdbc.ClientDataSource" is-isolation-level-
guaranteed="false"> <property name="PortNumber" value="1527"/>
<property name="Password" value="APP"/>
<property name="User" value="APP"/> <property name="serverName" value="localhost"/>
<property name="DatabaseName" value="sun-appserv-samples"/>
<property name="connectionAttributes" value=";create=true"/>
</jdbc-connection-pool>
-<jdbc-connection-pool name="PostSQLPool" res-type="javax.sql.DataSource"
datasource-classname="org.postgresql.ds.PGSimpleDataSource" ping="true">
<property name="databaseName" value="PostSQLDatenbank"/>
<property name="password" value="philipp"/> <property name="portNumber"
value="5432"/>
<property name="serverName" value="localhost"/>
<property name="user" value="postgres"/>
<property name="URL" value="URL=jdbc:postgresql://localhost:5432/Time"/>
</jdbc-connection-pool> -
<jdbc-connection-pool name="PostPool" res-
type="javax.sql.DataSource" datasource-
classname="org.postgresql.ds.PGSimpleDataSource" ping="true">
<property name="Url" value="jdbc:postgresql://localhost:5432/PostSQLDatenbank"/>
<property name="URL" value="jdbc:postgresql://localhost:5432/PostSQLDatenbank"/>
<property name="databaseName" value="PostSQLDatenbank"/>
<property name="password" value="philipp"/>
<property name="portNumber" value="5432"/>
<property name="serverName" value="localhost"/>
<property name="user" value="postgres"/>
</jdbc-connection-pool>
</resources>
答案 0 :(得分:0)
从您的堆栈跟踪中,您似乎在Glassfish之外运行,以使用您需要在服务器内运行的Glassfish数据源。
如果要在Glassfish之外运行,则需要直接指定JDBC连接URL。
请参阅, http://en.wikibooks.org/wiki/Java_Persistence/Runtime#Java_Standard_Edition