Can't get glassfish to connect to mysql - 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>

Related

Unable to access mysql from jpa after Catalina update

My jpa application was working fine (in fact, my 2 jpa applications) since I update my mac to Catalina (and restart it, of course).
Since then I got
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : null
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
...
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
But from mySQL Workbench I can access my databases with no problem.
I've tried to reinstall mysql, even using brew to do it. Nothing works.
I changed nothing on my persistence.xml file nor my code.
<?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="MyProject" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myschema"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="MyPassword"/>
<property name="javax.persistence.schema-generation.database.action" value="update"/>
</properties>
</persistence-unit>
</persistence>
My mysql version is 8.0.19.
Thank you!
Next day update (28/4):
By now, I have:
1) Added this line to my persistence.xml:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
As I got a new error...
Access denied for user 'root'#'localhost' (using password: YES)
2) Followed this instructions to set root password:
https://medium.com/#aungzanbaw/how-to-reset-root-user-password-in-mysql-8-0-a5c328d098a8
Now my new error is
Connection refused
Next next day update (29/4):
Today I stoped and started mysql from terminal:
sudo /usr/local/bin/mysql.server stop
sudo /usr/local/bin/mysql.server start
I then I got a new error
The server time zone value 'CEST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
Let's continue my investigation!
Following this question: https://es.stackoverflow.com/questions/48935/configurar-zona-horaria-jdbc-driver-java/48946?newreg=494e1840d4404575a81dc4ec10200266
I modified persistence.xml:
So I got a
Table 'myschema.mytable' doesn't exist
error.
Finally I got it, not sure how... probably restarting from comand line.
Here my final 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="MyProject" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myschema?serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="MyPassword"/>
<property name="javax.persistence.schema-generation.database.action" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
</properties>
</persistence-unit>
</persistence>
Using MySQL8Dialect is necessary to be able to create the tables without that error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=MyISAM' at line 1

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?

Netbeans/Glassfish - not connecting with jdbc using sun-resources.xml

I'm trying to get a Netbeans/Glassfish Enterprise application to run locally on my pc (it is currently working in production). I've gotten as far as the login display, but when I try to log in, the app throws the following exception:
SEVERE: "com.sun.enterprise.security.auth.login.common.LoginException: Login failed: javax.security.auth.login.LoginException: Failed file login for myloginname".
And the login servlet forwards to a login error page.
The web.xml in the "-web" project looks like this:
<resource-ref>
<res-ref-name>jdbc/my_name_MySQL</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
There's also a persistence.xml set up as follows:
<?xml version="1.0" encoding="UTF-8"?>
<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="modCompJar-PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/my_name_MySQL</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>
<property name="hibernate.search.default.indexBase" value="/opt/index/webInput"/>
</properties>
</persistence-unit>
</persistence>
Finally, there's a sun-resources.xml, which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/my_name_MySQL" object-type="user" pool-name="MyPool">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.Driver" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="MyPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="URL" value="jdbc:mysql://localhost:33306/mydatabsename?relaxAutoCommit="true""/>
<property name="User" value="root"/>
</jdbc-connection-pool>
</resources>
Note I've set up a tunnel via Putty to use a remote database, thus the "33306" for the mysql port instead of 3306.
I also changed Netbeans to point to the remote server's database, using the tunneled port. That works because the clicking on the "Database" node under the "Services" shows the same databases as exist on the remote server.
I know the login is correct because when I log in to the application running on the server, I get in, and they're both accessing the same database.
I also noticed there are some jdbc connection pools set up in "domain.xml" which refer to non-existent databases and ip addresses. The only valid database is the one shown above, in "sun-resources.xml".
How can I get this app to connect to the right database?

Tomcat 7 (JBoss EWS 2.0) + MySQL 5.1 + Hibernate - OpenShift - No persistence unit found

Every time I try to reach my app on OpenShift I receive the "No Persistence provider for EntityManager named appname" error.
I have been struggling to resolve this problem for two consecutive days and I still did not find an answer.
I have tried all the possible configuration I found over the web.
My current configuration:
META-INF\persistence.xml
<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="appname" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/comp/env/jdbc/appname</jta-data-source>
<class>myorg.mypackage.MyClass</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
</properties>
</persistence-unit>
</persistence>
.openshift\config\context.xml
<Context>
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/appname" password="mypass"
type="javax.sql.DataSource" url="jdbc:mysql://myurl:3306/appname"
username="myuser" />
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
.openshift\config\server.xml
<Host name="localhost" appBase="webapps" unpackWARs="false" autoDeploy="true">
<Context docBase="appname" path="/appname" reloadable="true" source="org.eclipse.jst.jee.server:appname" />
</Host>
The exception
javax.persistence.PersistenceException: No Persistence provider for EntityManager named appname
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at org.andvicoso.shopand.model.dao.base.GenericDaoJPA.<init>(GenericDaoJPA.java:20)
at org.andvicoso.shopand.model.dao.ProductDaoJPA.<init>(ProductDaoJPA.java:8)
at org.andvicoso.shopand.web.ViewHelper.getProductDao(ViewHelper.java:31)
at org.andvicoso.shopand.web.ViewHelper.getHighlights(ViewHelper.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:106)
at org.apache.jsp.index_jsp._jspx_meth_c_005fset_005f6(index_jsp.java:260)
Can anyone help me???
Thanks in advance.
I faced this issue and found that my META-INF was under src\main\java which was causing this error. When I moved META-INF to src\main\resources... it worked.

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.