persistence exception on database connection using glassfish - mysql

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.

Related

Wilfdly not picking up mysql hibernate configuration in persistence.xml

i'm having difficulty get Wildfly to use MySQL as a database, no connections are made. I have the following in the persistence.xml file:
<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="primary">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/X" />
<property name="javax.persistence.jdbc.user" value="X" />
<property name="javax.persistence.jdbc.password" value="X" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Changing e.g the dialect shows up in the logfiles, changing the persistence-unit name also affects the application so the persistence.xml is picked up except for the mysql part.
Any help appreciated,
--
Michel

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.

Unable to see queue information or messages in activemq webconsole

I'm trying to configure Activemq with MySql. Everything seems to have been configured properly as i am able to send and receive messages through code. But I'm not able to see any queue or messages information in the Activemq Web Console.
below is my activemq config . Any help is apprciated.
Thank you!
Activemq.xml:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:${activemq.conf}/credentials.properties</value>
</property>
</bean>
<broker useJmx="false" brokerName="jdbcBroker" xmlns="http://activemq.apache.org/schema/core">
<persistenceAdapter>
<jdbcPersistenceAdapter dataDirectory="${activemq.base}/activemq-data" dataSource="#mysql-ds"/>
</persistenceAdapter>
<transportConnectors>
<transportConnector name="default" uri="tcp://0.0.0.0:61616"/>
</transportConnectors>
</broker>
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
<property name="username" value="admin"/>
<property name="password" value="root"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
<import resource="jetty.xml"/>
</beans>
As the WebConsole is using JMX to retrieve data, you should try to remove useJmx="false" from your broker configuration.

Cannot adding a table in Database via persistence.xml using Hibernate Jpa , Tomcat and Mysql

I'm working in flex, blazeds,hibernate jpa, tomcat and mysql.
This is the problem that I face when I start the project:
05.06.2012 22:33:29.459 INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.4.0.GA
05.06.2012 22:33:29.479 INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
05.06.2012 22:33:29.482 INFO org.hibernate.cfg.Environment - hibernate.properties not found
05.06.2012 22:33:29.487 INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
05.06.2012 22:33:29.493 INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
05.06.2012 22:33:29.645 INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.1.0.GA
05.06.2012 22:33:29.650 INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.4.0.GA
And this is my persistence.xml file under src/META-INF:
<persistence version="1.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_1_0.xsd">
<persistence-unit name="consultant_db">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.adobe.demo.domain.Permission</class>
<class>com.adobe.demo.domain.Role</class>
<class>com.adobe.demo.domain.User</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/consultant_db" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.pool_size" value="6" />
<property name="hibernate.connection.autoReconnect" value="true" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
But if I delete the line: <class>com.adobe.demo.domain.User</class>
the other 2 tables will be created.
What should I do?
Here is a similar question. The OP was trying to create a "User" table, which failed. The OP was able to resolve the issue by annotation his User class with #Table(name = "\"user\"").
I also gave some tips on how to troubleshoot such problems.

OpenJPA + MySQL issue

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.