OpenJPA + MySQL issue - mysql

I have been using OpenJPA with MySQL. Here is my persistence.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!--
For DB connectivity
-->
<persistence version="1.0">
<persistence-unit name="jpa">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.Login</class>
</persistence-unit>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/jpa"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName" value="root"/>
<property name="openjpa.ConnectionPassword" value="root"/>
<property name="openjpa.jdbc.DBDictionary" value="mysql(SimulateLocking=true)"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
</properties>
</persistence>
When I am executing my Java class, I am getting an exception which is given below. The same code is working fine if I use hibernate and change configuration in persistence.xml, But it is not working in OpenJPA. Can anyone help to solve this?
Exception in thread "main" javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for "jpa" after trying the following discovered implementations: org.apache.openjpa.persistence.PersistenceProviderImpl from provider: org.apache.openjpa.persistence.PersistenceProviderImpl
at javax.persistence.Persistence.createPersistenceException(Persistence.java:244)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:186)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:72)
at com.JPALogin.main(JPALogin.java:26)
Caused by: <openjpa-2.2.0-r422266:1244990 nonfatal general error> org.apache.openjpa.util.GeneralException: org.xml.sax.SAXException: file:/E:/work/JPAPlain/build/classes/META-INF/persistence.xml [Location: Line: 5, C: 28]: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'persistence'.
at org.apache.openjpa.lib.meta.XMLMetaDataParser.parseNewResource(XMLMetaDataParser.java:427)
at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:347)
at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:324)
at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:297)
at org.apache.openjpa.persistence.PersistenceProductDerivation$ConfigurationParser.parse(PersistenceProductDerivation.java:772)
at org.apache.openjpa.persistence.PersistenceProductDerivation.parseResources(PersistenceProductDerivation.java:556)
at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:522)
at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:336)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:86)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:153)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:62)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:152)
... 2 more

How about you try something like this?
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<!-- For DB connectivity -->
<persistence-unit name="jpa">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.Login</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/jpa" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="root" />
<property name="openjpa.jdbc.DBDictionary" value="mysql(SimulateLocking=true)" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
</persistence>

SAXParseException: cvc-elt.1: Cannot find the declaration of element 'persistence'. at org.apache.openjpa.lib.meta.XMLMetaDataParser.parseNewResource
I can't be sure, but it looks like the XML parser of your persistence.xml file is trying to validate the XML against a DTD, and it is complaining because it can't get a (useful) DTD. You might want to change the first line of your XML file to the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
I would not expect an invalid XML file or missing DTD to cause the parser to crash with a mysterious exception. So if that is the cause of the problem, I'd say the persistence provider code had a bug in it. I'd expect a more useful exception, with a message saying "invalid XML", or some such.

Related

Wildfly 10 persistence MySQL table not found

I have a REST service which access a MySQL database. I'm using Wildfly 10 and MySQL 5.7.12. I am trying to get the EntityManager as an injection and I get the following error when executing the find method for my Entity mapping the table content.
org.h2.jdbc.JdbcSQLException: Table "MYTABLE" not found; SQL statement:
In the RESTService class I have
#PersistenceContext(unitName="myUnit")
protected EntityManager entityManager;
and my persistence.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="myUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mytable" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="pass" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
</properties>
</persistence-unit>
</persistence>
The issue is that if instead of using injection I retrieve the entity manager using the manual way everything works smothly.
EntityManagerFactory emFactory;
emFactory = Persistence.createEntityManagerFactory("myUnit");
EntityManager em = emFactory.createEntityManager();
Could you give me some hints on how to use the PersistenceContext? the code is somehow cleaner and I prefer to use it.
It looks like you 're getting the default datasource injected in your persistence Unit so I guess this depends on how the EntityManager is 'built'. One way to fix this is to create a datasource in WidFly and use it (through) its JNDI name in your persistence unit.
Feel free to report a bug http://issues.jboss.org/
You are setting up a RESOURCE_LOCAL persistence unit. You should configure it as such:
<persistence-unit transaction-type="RESOURCE_LOCAL">
In order to use a resource local persistence unit you cannot inject EntityManager, only EntityManagerFactory. You'll end up with a lot less plumbing if you switch to JTA datasource and let the server manage it.
If you absolutely don't want to edit standalone.xml, in WF8 anyway, you can drop yourdatasource-ds.xml file into your WEB-INF folder, or into the deployments directory alongside your .war file. There was talk of removing this from WF though so I don't know if it works in 10.x.

Connecting to remote database server

I'm developing an application in a container managed environment (Java EE). Therefore, I shouldn't have to worry about handling and closing transactions. I simply inject the EntityManager:
#PersistenceContext(unitName = "WebApplication")
private EntityManager em;
And my corresponding persistence.xml 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="WebApplication" transaction-type="JTA">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://link-to-my-database.us-east-1.rds.amazonaws.com:3306/TestDB"></property>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
However, this does not work. According to the JPA 2.0 specification, javax.persistence.jdbc.driver is "attended for use in Java SE environments." (Section 8.2.1.9; page 317). And even the jta-datasource and non-jta-datasource only "name the data source in the local environment" (Section 8.2.1.5; page 313).
Then how can I connect to a remote database server? Obviously, I can explicitly create an EntityManager but then I would be responsible for handling the transactions, then what's the point of being in a container managed environment? If it's possible to connect to a remote database server using JPA and a container managed environment, can you provide an example on how to do so?

Play 2.3 JPA Hibernate MySQL Pooling/Connection Error

I'm developing a Play 2.3 Application with JPA/Hibernate and use a Heroku/ClearDB.com MySQL Database for this but after the application connects successfully to the DB I get following error:
[error] c.j.b.h.AbstractConnectionHook - Failed to acquire connection to jdbc:mysql://eu-cdbr-west-01.cleardb.com:3306/heroku_b7ea7b2d2972532 Sleeping for 1000ms and trying again. Attempts left: 10. Exception: null.Message:User 'badd25925bdd5f' has exceeded the 'max_user_connections' resource (current value: 10)
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: User 'badd25925bdd5f' has exceeded the 'max_user_connections' resource (current value: 10)
I tried almost everything I saw on other posts but nothing helped... I think it has to do with pooling...?
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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://eu-cdbr-west-01.cleardb.com:3306/heroku_b7ea7b2d2972532?reconnect=true"/>
<property name="javax.persistence.jdbc.user" value="badd25925bdd5f"/>
<property name="javax.persistence.jdbc.password" value="XXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.c3p0.min_size" value="2"/>
<property name="hibernate.c3p0.max_size" value="9"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
</properties>
</persistence-unit>
</persistence>
application.conf (relevant part)
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://eu-cdbr-west-01.cleardb.com:3306/heroku_b7ea7b2d2972532"
db.default.user="badd25925bdd5f"
db.default.password="XXX"
jpa.default=defaultPersistenceUnit
Thanks a lot for your help!
I don't know about using Hibernate, but with Ebean the connection pool by default is managed by BoneCP. Try to configure it in your application.conf:
# Max connections = partitionCount * maxConnPerPartition
db.default.partitionCount=2
db.default.maxConnectionsPerPartition=5
or in your persistence.xml:
<property name="bonecp.partitionCount" value="2" />
<property name="bonecp.maxConnectionsPerPartition" value="5" />
The error says that you are using more connections then your plan .So you can
Either upgrade you db plans see https://addons.heroku.com/cleardb#ignite
or
You can try scaling down your aplication see https://devcenter.heroku.com/articles/scaling
Solution to this problem (for Play 2.3) was to add the play-hikaricp library to the application.

Can't get glassfish to connect to mysql

SEVERE: Local Exception Stack: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseExceptionInternal 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.Error Code: 0
<?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="HelloWorld">
<class>model.HelloWorld</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/database" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
I have the connector install on glassfish, with successful pings I just can't the servlet and entitymanager to get the query to the mysql server. Any help would be good, also tutorials as well.
If you are using a Glassfish-initiated JDBC connection (I think you are since you say you sucessfully pinged the datasource), then you don't want to define the properties as you have them in the persistence.xml. You'll want to just specify a data-source by JNDI name.
<?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="HelloWorld">
<non-jta-data-source>jdbc/myds</non-jta-data-source>
<class>model.HelloWorld</class>
<properties>
</properties>
</persistence-unit>
</persistence>

persistence exception on database connection using glassfish

I'm new to glass-fish & persistence and while trying to run a query on MySQL database in my machine i get the following error:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: No database selected
Error Code: 1046
before running the server i made sure that the server configuration are done using this guide : mysql site manual for using connector with glassfish, and configured my persistence.xml like this:
<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="StudentRecipieWebsite" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/mySql</non-jta-data-source>
<class>il.musehunter.studentRecipes.dbModel.Image</class>
<class>il.musehunter.studentRecipes.dbModel.Ingrediant</class>
<class>il.musehunter.studentRecipes.dbModel.Recipe</class>
<class>il.musehunter.studentRecipes.dbModel.User</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/recipes_data"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="moshe1475"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
</properties>
</persistence-unit>
I'm really new to this and does not have a clue to what i should do next so any help would be very appreciated.
edit:
adding part of the domain.xml from my glassfish server :
<applications>
<application context-root="/StudentRecipieWebsite" location="${com.sun.aas.instanceRootURI}/applications/StudentRecipieWebsite/" name="StudentRecipieWebsite" object-type="user">
<property name="appLocation" value="${com.sun.aas.instanceRootURI}/applications/__internal/StudentRecipieWebsite/StudentRecipieWebsite.war"></property>
<property name="org.glassfish.ejb.container.application_unique_id" value="89812698949156864"></property>
<property name="org.glassfish.persistence.app_name_property" value="StudentRecipieWebsite"></property>
<property name="defaultAppName" value="StudentRecipieWebsite"></property>
<module name="StudentRecipieWebsite">
<engine sniffer="ejb"></engine>
<engine sniffer="security"></engine>
<engine sniffer="jpa"></engine>
<engine sniffer="web"></engine>
</module>
</application>
and:
<resources>
<jdbc-resource pool-name="__TimerPool" jndi-name="jdbc/__TimerPool" object-type="system-admin"></jdbc-resource>
<jdbc-resource pool-name="DerbyPool" jndi-name="jdbc/__default"></jdbc-resource>
<jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.EmbeddedXADataSource" res-type="javax.sql.XADataSource" name="__TimerPool">
<property name="databaseName" value="${com.sun.aas.instanceRoot}/lib/databases/ejbtimer"></property>
<property name="connectionAttributes" value=";create=true"></property>
</jdbc-connection-pool>
<jdbc-connection-pool is-isolation-level-guaranteed="false" datasource-classname="org.apache.derby.jdbc.ClientDataSource" res-type="javax.sql.DataSource" name="DerbyPool">
<property name="PortNumber" value="1527"></property>
<property name="Password" value="APP"></property>
<property name="User" value="APP"></property>
<property name="serverName" value="localhost"></property>
<property name="DatabaseName" value="sun-appserv-samples"></property>
<property name="connectionAttributes" value=";create=true"></property>
</jdbc-connection-pool>
<jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" res-type="javax.sql.DataSource" name="MySQLConnPool">
<property name="portNumber" value="3306"></property>
<property name="databaseName" value="recipes_data"></property>
<property name="serverName" value="localhost"></property>
<property name="user" value="root"></property>
<property name="password" value="moshe1475"></property>
<property name="URL" value="jdbc:mysql://:3306/"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="MySQLConnPool" description="" jndi-name="jdbc/mySql"></jdbc-resource></resources>
My first guess is that the connection URL in the data source definition is not quite right because "No database selected Error Code: 1046" is a MySQL error.
You do not need to configure the data source in persistence.xml if you have already defined a data source in your application server (which is the common way). Can you post the data source definition from your $GLASSFISH_HOME/glassfish/domains/$GLASSFISH_DOMAIN/config/domain.xml?
This is an example persistence.xml I took from an project of mine (more or less from the Netbeans template) that might help you:
<?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/sample</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Good luck!
Answering to the comment:
You need to add the database name to the URL:
<property name="URL" value="jdbc:mysql://nameOrIPOfYourServerOrLocalhost:3306/nameOfYourDatabase"></property>
That should resolve the problem.