EclipseLink 2.5 not consulting persistence.xml - java

I created a Struts 2 web app using a maven archetype. Now, when I deployed that project on tomcat7 and edited persistence.xml to change database config, it is not reflecting in Application. To test my theory, I removed persistence.xml from WEB-INF/classes/META-INF/. Even then what I am getting is this:-
Connection refused: connect
java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect. Error Code: 40000
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect. Error Code: 40000
I don't have any idea from what location it is fetching the Persistence configuration. Here is my persistence.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="em" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.ankitsoft.entities.City</class>
<class>com.ankitsoft.entities.Country</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:C:\proLoc\WEB-INF\DB"/>
<property name="javax.persistence.jdbc.password" value="ankit"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value="ankit"/>
</properties>
</persistence-unit>
</persistence>
Can anybody please help me?

If you are calling the persistence.xml in a bundle, then org.eclipse.persistence.jpa must be started before your bundle (or bundles). It is therefore a good idea to always add org.eclipse.persistence as an imported package in your manifest

Related

access denied when accessing mysql database through glassfish server

I'm following a tutorial which uses GlassFish to access a mysql database in NetBeans. The problem is I'm getting this error
Internal Exception: java.sql.SQLException: Error in allocating a
connection. Cause: Connection could not be allocated because: Access
denied for user 'root'#'localhost' (using password: NO)
when I try to deploy my app. I understand I need to add the password to the configuration so that GlassFish can establish the connection. The problem is I'm not sure where to include it within the persistence file.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.mycompany_MyApp_ejb_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>jdbc/creditpurchase</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
You can edit properties in glassfish admin console. Resources->JDBC->Connections Pools->your_connection_pool in Additional Properties tab. You can add property with name password. take a look here.
I was able to do it here:
C:\Code\Java\Glassfish\glassfish3\glassfish\domains\domain1\config\domain.xml
Change the properties within the jdbc-connection-pool tag

jdbc sql server connection error on dev server

I am using tomee, JPA and find below the configuration:
tomee.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="dataSource" type="DataSource">
JdbcDriver com.microsoft.sqlserver.jdbc.SQLServerDriver
JdbcUrl jdbc:sqlserver://******:1433;databaseName=******
UserName ******
Password ******
JtaManaged true
</Resource>
</tomee>
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="app">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>dataSource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
This is working fine on my local. But when I tried to deploy my application to Dev Linux server, I am getting this exception WARN: HHH000342: Could not obtain connection to query metadata : The TCP/IP connection to the host ******, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
The only difference is localhost is windows machine and dev server is Linux machine. As far as I know for this there are no changes required in connection string except the host name.
I also tried with DB server IP address instead of host name. Now server is stuck at this point: INFO: HHH000130: Instantiating explicit connection provider:org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
Please give your suggestions.
I tried with jtds and it worked. So I think there might be some issues if we use sql jdbc driver in linux.
There was one more issue. When I tried with jtds, I got this exception: java.lang.UnsupportedClassVersionError: net/sourceforge/jtds/jdbc/Driver : Unsupported major.minor version 51.0. My local JDK was 1.7 and Dev server JDK was 1.6. And I tried to deploy the war generated from my local to Dev server. Once I changed server JDK to 1.7 this issue also got resolved.
There’s a Windows service that runs on the database server called the “SQL Server Browser” that needed to be restarted. The service was listed as running, but it wasn’t responding to queries. As soon as we restarted this service everything started working again. Here’s some more information on this service.
http://msdn.microsoft.com/en-us/library/hh510203.aspx

Generating tables from entities

I am trying to generate tables from my entities in Eclipse Juno using Project right-click -> JPA Tools -> Generate Tables from Entities
I am using EclipseLink as JPA Provider
My app container is Glassfish 4
My database is the one embedded within glassfish, derby.
My application starts without errors and I am able to commit data to the database using a simple test bean (so I assume my connection pool and datasource are working as intended)
I am getting the following stack trace error when I try to generate the tables from entities:
[EL Info]: EclipseLink, version: Eclipsenter code heree Persistence Services - 2.4.0.v20120608-r11652
[EL Severe]: ejb: Local Exception Stack:
Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:502)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:109)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:685)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:213)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:278)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:282)
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.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
... 11 more
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:602)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:278)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:282)
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: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:502)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:109)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:685)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:213)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
... 7 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
... 11 more
Here is my persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="planificateurepicerie" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__planificateur</jta-data-source>
<class>com.whybe.pe.jpa.Ingredient</class>
<class>com.whybe.pe.jpa.Recette</class>
<class>com.whybe.pe.jpa.ComposantRecette</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
I also have the database setup in the Database Development perspective and that works fine too.
Inside the glassfish console I can ping the connection pool successfully.
I am running out of ideas as of why the ddl won't generate...
If you need more details let me know!
Thanks in advance!
You are using transaction-type="JTA" in a run mode as standalone application. JPA looking for the JNDI resource name in the default context (Wait, currently there is no context in your application, since is a run mode standalone application). Change to transaction-type="LOCAL" for that run mode. e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SamplePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
<property name="javax.persistence.jdbc.password" value="123"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
But you are using Glassfish 4. Run the application on the server. If you don't have the JDNI resource name, you need to add it in the JDBC resources. The more easy way is using the console administration of Glassfish. By default, you can find it in http://localhost:4848. See more about this in Chapter 3 JDBC Resources.

Injected Entity Manager not using correct persistence unit

I have a JAX-RS restful service which needs to access a MySQL database. I am trying to do this using CDI and a entity manager. However, when I publish the app, it appears that the incorrect persistence unit is being used (it's trying to connect on port 1527 instead of 3306).
The exception that is caught by the try/catch is:
javax.servlet.ServletException:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
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.
Error Code: 0
Here is the restful service:
#Path("/databases")
#Stateless
public class DatabaseResource {
#PersistenceUnit(unitName = "beta.example.services")
EntityManagerFactory entityManagerFactory;
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response list() {
try {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
return Response.ok().build();
} catch (SchemaCrawlerException e) {
return Response.status(500).entity(e.getMessage()).build();
}
}
}
The persistence unit (located in src/META-INF):
<?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="beta.example.services">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/" />
<property name="javax.persistence.jdbc.user" value="test" />
<property name="javax.persistence.jdbc.password" value="test" />
</properties>
</persistence-unit>
</persistence>
Any help would be appreciated
Ok, a persistence unit can be configured in two modes : RESOURCE_LOCAL and JTA.
Ex:
<persistence-unit name="beta.example.services" transaction-type="JTA">
VS
<persistence-unit name="beta.example.services" transaction-type="RESOURCE_LOCAL">
JTA is the default value. Properties like "javax.persistence.jdbc.*" will only be read when using "RESOURCE_LOCAL". When using JTA, the transaction manager of glassfish will be used. That means in this case you must specify JNDI name like this :
<jta-data-source>youJNDIName</jta-data-source>
What I think might be happening is that you use the default "JTA" transaction-type, but since you dont specify any jta-datasource, it might try to use the glassfish default one (wich point to derby).
Persistence unit as RESOURCE_LOCAL or JTA?
http://openejb.apache.org/jpa-concepts.html
Looking at how you use the entity manager, setting transaction-type to RESOURCE_LOCAL seems to be the solution for you.

Entity manager error on the server?

Hi I have developed my java application on glassfish server with EJB 3.0 persistance. On my local machine the application was running fine and was persisting the data perfectly. However, When I deployed it on the linux server, whenever I persist any data it gives me this EJB exception error:
Caused by: Java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
And here is my persistence.xml file:
<?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="MyPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/security</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
what could possbily cause this?
PS: Im using Glassfish 3.1.43, EJB 3.0
What database are you using? I would verify that the user you have configured in the connection pool has the privilege to create tables.
I have managed to solve it finally!! The problem was in the database connection that was not set properly!! Now I have connected to the database and the data are persisted!!

Categories