access denied when accessing mysql database through glassfish server - java

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

Related

Configuration of persistence.xml while using websphere 8.5.5.1

I'm coding an app that uses Servlet 3.0, Jsp, and JPA 2.0 and i'm deploying it into Websphere application server 8.5.
Since i already configured into the ibm websphere console, the data source and the jdbc driver, and the j2c authentification (i'm using oracle 11g as a database ). I dont know how my persistence.xml should look like, if i need to specify and add openJPA jars to my project.
For now anything i put into persistence.xml i'm having this issue :
Error 500: <openjpa-2.2.3-SNAPSHOT-r422266:1764177 fatal user error> org.apache.openjpa.persistence.ArgumentException
What should i do ? maybe i'm missing how JPA works
Thanks in advance
The OpenJPA jars should be provided by WebSphere and available to use for your application. There is a JPA sample available here: https://developer.ibm.com/wasdev/downloads/#asset/samples-Java_Persistence_API_JPA_Sample
In the sample, you can see an example of the 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="jpasamplepersistenceunit">
<jta-data-source>java:comp/env/jdbc/samplejpadatasource</jta-data-source>
<non-jta-data-source>java:comp/env/jdbc/samplejpadatasourcenonjta</non-jta-data-source>
<class>wasdev.sample.jpa.Thing</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- These properties are creating the database on the fly. We are using them to avoid users having
to create a database to run the sample.
See also the create=true line in the datasource meta data. -->
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="derby" />
<!-- EclipseLink specific properties to create the database. They are only used if using the jpa-2.1 feature. -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="both" />
</properties>
</persistence-unit>
</persistence>
I think your error is not caused by the OpenJPA jars not being available. It might be because your database is not configured correctly. Make sure your persistence.xml file refers to your datasources properly.

Get JTA DataSource properties

I would like to get my DataSource properties, like user name or url.
This is my persistence.xml:
<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="defaulttransaction-type="JTA">
<jta-data-source>MyDataSource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
Inside GlassFish 4.1, I have the required JDBC Resource and JDBC ConnectionPool, respectively MyDataSource and MyDataSourceCP.
If this is useful, MyDataSource is a javax.sql.ConnectionPoolDataSource and MyDataSourceCP is a oracle.jdbc.pool.OracleConnectionPoolDataSource.
MyDataSourceCP is configured with some parameters like URL, user name and password.
I would like to extract those properties from Java.
So far, I have tried the following:
How to get jpa datasource properties from Entity Manager
How to retrieve the datasource used by a persistence unit programmatically
JPA - Is there a way/method to retrieve Persistence Unit information
But with no results.
As #Stoffelchen (in (1)), I would like something like
someObject.someMethod( ).getProperties( )
// or
someObject.someMethod( ).getProperties( "username" )
// or
someObject.someMethod( ).getUserName( )
Is there a way to achieve this?
I am using Hibernate 4.3.
Thanks in advance

Persistence.xml file for Hibernate JPA and Mysql

I'm finding it difficult to find an up-to-date and clear explanation of how the persistence.xml should look and what it needs to contain. What would your typical persistence.xml file look like for the following:
Provider: Hibernate implementation of JPA
Mysql database
DB name: hibernatetest
username: root
password: 12345
databases url: localhost port 3306
and for the sake of simplicity say that my Entity class that needs to be persistent is located at com.example.MyClass
Thanks in advance!
here is an basic persistance.xml example :
<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_2_0.xsd"
version="2.0">
<persistence-unit name="org.hibernate.tutorial.jpa">
<class>com.example.MyClass</class>
<class>...</class>
<class>...</class>
</persistence-unit>
</persistence>
You can find more details about it here :
http://docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/hibernate-gsg-tutorial-jpa.html

JPA MySQL Connection in Java EE project

I am running a local MySQL Server, later I want to use one on a remote computer and I have a question how to setup JPA in my Java EE project correctly.
First of all, do I have to setup the database connection in my application server or only in my persistence.xml? I have just added it in a JBoss Server and I can use JNDI now. But JNDI is not compatible with JPA, is it correct?
I tried it now with Geronimo and JBoss, with both I am not sure what I have to do correctly. I wanted to use OpenJPA as an JPA implementation which is standard on Geronimo.
I found many tutorials on the web, but most isnt very complet.
<?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="schulungen" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>MySQLServer</jta-data-source>
<class>model.Schulung</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
</properties>
</persistence-unit>
</persistence>
What is JTA-Data-Source? If I understood it right, it's a name given by the application server to identify the which connection I want.
Do I have to add properties like jdbc connection URL etc.?
I hope you understand my problem.

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