Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [glassfish-dev] How to use Glassfish with MySQL or any database instead of Apache Derby

Em 2024-04-26 10:29, Victor Gimenez via glassfish-dev escreveu:
Em 2024-04-26 01:15, Hiroki Sawamura (Fujitsu) escreveu:
Hi,

Hi! Thanks again for the reply.


I did another test to verify taking all the <persistence-unit name="MySqlUnit"> XML block on persistence.xml and I received another message:

```
Error occurred during deployment: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [dukes.greeting.GreetingRepository/em] in the scope of the module called [challenge2]. Please verify your application.. Please see server.log for more details.
```

[challenge2] : JNDI lookup failed for the resource: Name: MySqlUnit,
Lookup: jdbc/mysql, Type: javax.sql.DataSource -- Lookup failed for
jdbc/mysql in

Hm, it seems that for JPA the resource type should be javax.sql.DataSource. so, try changing the JDBC connection pool resource type to "javax.sql.DataSource" and the data source class name to "com.mysql.cj.jdbc.MysqlDataSource".



With javax.sql.DataSource as JDBC connection pool and com.mysql.cj.jdbc.MysqlDataSource I am facing the same thing:

```An error has occurred Error occurred during deployment: Exception while deploying the app [challenge2] : JNDI lookup failed for the resource: Name: MySqlUnit, Lookup: jdbc/mysql, Type: javax.sql.DataSource -- Lookup failed for jdbc/mysql in SerialContext[myEnv={java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}]. Please see server.log for more details. An error has occurred```

And at the server.log almost the same:
```
[2024-04-26T10:26:24.547397-03:00] [GF 7.0.13] [SEVERE] [] [org.glassfish.admin.rest.resources.TemplateListOfResource] [tid: _ThreadID=90 _ThreadName=admin-listener(3)] [levelValue: 1000] [[ Error occurred during deployment: Exception while deploying the app [challenge2] : JNDI lookup failed for the resource: Name: MySqlUnit, Lookup: jdbc/mysql, Type: javax.sql.DataSource -- Lookup failed for jdbc/mysql in SerialContext[myEnv={java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}]. Please see server.log for more details.]]

[2024-04-26T10:26:24.548382-03:00] [GF 7.0.13] [INFO] [] [org.glassfish.admingui] [tid: _ThreadID=92 _ThreadName=admin-listener(5)] [levelValue: 800] [[ Exception Occurred :Error occurred during deployment: Exception while deploying the app [challenge2] : JNDI lookup failed for the resource: Name: MySqlUnit, Lookup: jdbc/mysql, Type: javax.sql.DataSource -- Lookup failed for jdbc/mysql in SerialContext[myEnv={java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}]. Please see server.log for more details.]]
```

I am trying to discover if there's any configuration which I should to do in persistence.xml or there is really any parameter on the configuration pool which should I do.


Thank you,

Victor

-----Original Message-----
From: Victor Gimenez <victor.gimenez@xxxxxxxxxxxx>
Sent: Friday, April 26, 2024 8:47 AM
To: Sawamura, Hiroki/澤村 大輝 <sawamura.hiroki@xxxxxxxxxxx>
Subject: Re: [glassfish-dev] How to use Glassfish with MySQL or any database instead of Apache Derby

Em 2024-04-25 05:33, Hiroki Sawamura (Fujitsu) escreveu:
Hi

Hi!

Humm, it appears that the package of the MySQL implementation class has
changed.
 - mysql-connector-java-8.0.21.jar:
com/mysql/cj/jdbc/MysqlConnectionPoolDataSource.class
so, the data source class name that should be specified for the JDBC
connection pool is "com.mysql.cj.jdbc.MysqlConnectionPoolDataSource"
instead of "
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource".

It worked here after that I changed this data source class name!!

I am not familiar with JPA, but for JPA, you would specify the JDBC
resource name as a data source in persistence.xml and then inject its
unit name into EntityManager.
```persistence.xml
<persistence //snip//>
  <persistence-unit name="MysqlUnit">
    <jta-data-source>jdbc/mysql</jta-data-source>
  </persistence-unit>
</persistence>
```

```java
public class YourAppDao{
    @PersistenceContext(unitName="MysqlUnit")
    private EntityManager em;
```

I did this modifications here in my persistence.xml file and the .java
file which contains the EntityManager em variable declaration and it
stayed like that
```
persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence";
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence
https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd";
              version="3.0">

     <persistence-unit name="default">
         <!--transaction-type="JTA"-->
<!--
<jta-data-source>java:comp/DefaultDataSource</jta-data-source>-->
         <properties>
             <property
name="jakarta.persistence.schema-generation.database.action"
value="drop-and-create"/>
         </properties>
     </persistence-unit>

     <persistence-unit name="MySqlUnit">

<!--<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>-->
         <jta-data-source>jdbc/mysql</jta-data-source>
         <properties>
             <!-- connection data -->
			<property name="jakarta.persistence.jdbc.driver"
value="com.mysql.cj.jdbc.Driver" />
			<property name="jakarta.persistence.jdbc.url"
value="jdbc:mysql://localhost/app?createDatabaseIfNotExist=true" />
			<property name="jakarta.persistence.jdbc.user" value="root" />
			<property name="jakarta.persistence.jdbc.password" value="admin" />
         </properties>
     </persistence-unit>
</persistence>
```


```
java
...
...
@PersistenceContext(unitName="MysqlUnit")
     private EntityManager em;
...
...

```

The compilation with maven went good with mvn clear and mvn package
working

But when I am gonna deploy on Glassfish GUI I am getting:

```
Error occurred during deployment: Exception while deploying the app
[challenge2] : JNDI lookup failed for the resource: Name: MySqlUnit,
Lookup: jdbc/mysql, Type: javax.sql.DataSource -- Lookup failed for
jdbc/mysql in
SerialContext[myEnv={java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming}]. Please see
server.log for more details.
```

I am trying to realize what I am doing wrong!


Thank you,

Victor

-----Original Message-----
From: Victor Gimenez <victor.gimenez@xxxxxxxxxxxx>
Sent: Thursday, April 25, 2024 4:41 PM
To: Sawamura, Hiroki/澤村 大輝 <sawamura.hiroki@xxxxxxxxxxx>; Glassfish Dev
<glassfish-dev@xxxxxxxxxxx>
Subject: Re: [glassfish-dev] How to use Glassfish with MySQL or any
database instead of Apache Derby

Em 2024-04-24 22:13, Hiroki Sawamura (Fujitsu) escreveu:
Hi

Hi Hiroki!

Class name is wrong or classpath is not set for :
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource Please
check the server.log for more details.

Oops, I may have forgotten to mention something simple: placing the
JDBC driver under the lib is to add it to the classpath, so
DAS(GlassFish) restart is required.

I entered sudo ./asadmin restart-domain to restart the DAS(Glassfish)
and I successfully got this return statement:

```
Waiting finished after 30 ms.
Server instance is stopped, now we wait for the start on localhost:4848
...
Waiting finished after 2,695 ms.
Successfully restarted the domain
Command restart-domain executed successfully.

```

But when I did that ping command the same error persisted, I let the
mysql-connection-java-8.0.21.jar in
/home/victor/Downloads/web-7.0.13/glassfish7/glassfish/domains/domain1/lib/
path.

And about the step 4 referring these code snippets in which .java
file should I refer them?

It is simply example of how to reference the JDBC resource within your
Jakarta EE application deployed to GlassFish. It is not a specific
Java file.

And this ":" at the final of the code snippet is a ";" or "}" to
finish the if block?

No. It indicates that the description of any process would continue
after that.


Ohhhh now it made sense here to me and I realized, I remembered now
that this DataSource dataSource is a variable responsible for getting
the connection number as true or false to create the statement, and
after query, update, delete and so on... In JPA I am using
EntityManager em variable to make the CRUD operations like
insert(em.persist), list data (em.getCriteriaBuilder().createQuery...
do you know if I can able to use also the @Resource annotation above
EntityManager em declaration?


Hiroki

-----Original Message-----
From: Victor Gimenez <victor.gimenez@xxxxxxxxxxxx>
Sent: Thursday, April 25, 2024 3:41 AM
To: glassfish-dev@xxxxxxxxxxx
Cc: Sawamura, Hiroki/澤村 大輝 <sawamura.hiroki@xxxxxxxxxxx>
Subject: Re: [glassfish-dev] How to use Glassfish with MySQL or any
database instead of Apache Derby

Hello Hiroki! Thanks for your response,



I did the steps 1, 2 and 3 successfully obtaining as return:

```
JDBC connection pool mysql-pool created successfully.
Command create-jdbc-connection-pool executed successfully.
```

Only the connection check with ping returned me:
```
CLI031: Warning: Option "target" is obsolete and will be ignored.
CLI031: Warning: Option "target" is obsolete and will be ignored.
remote failure: Ping Connection Pool failed for
org.glassfish.resourcebase.resources.api.PoolInfo@bad58506[jndiName=my
sql-pool, applicationName=null, moduleName=null].
Class name is wrong or classpath is not set for :
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource Please
check the server.log for more details.
Command ping-connection-pool failed.
```

I don't know what is missing in my configurations or if I forgot to
set anything else.

And about the step 4 referring these code snippets in which .java file should I refer them? And this ":" at the final of the code snippet is
a ";" or "}" to finish the if block?


Thank you,


In 2024-04-24 02:56, Hiroki Sawamura (Fujitsu) wrote:
Hi,

There are 4 simple steps to setting up DB with GlassFish.

1. Place JDBC driver in DAS lib dir.
  $ mv mysql-connector-java-XXX.jar
glassfish7/glassfish/domains/domains/domain1/lib/

2. Create JDBC connection pool.
  $ asadmin create-jdbc-connection-pool \
    --restype javax.sql.ConnectionPoolDataSource \
    --datasourceclassname
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource \
    --property
user=your_username:password=your_password:port=3306:databaseName=your
_
database_name:serverName=localhost
\
    mysql-pool

  It would be a good to check the connection with ping.
    $ asadmin ping-connection-pool mysql-pool

3. Create JDBC resource.
  $ asadmin create-jdbc-resource --target server --connectionpoolid
mysql-pool jdbc/mysql

4. Refer in your application.
lookup is no longer needed. it can be easily injected and used with
jakarta.annotation.Resource annotation.
```java
  @Resource(lookup = "jdbc/mysql")
  DataSource dataSource;
```
then, describe the operation as follows ```java java.sql.Connection
con = dataSource.getConnection(); if (con.isValid(10)) {
	java.sql.Statement stmt = con.createStatement();
	java.sql.ResultSet result = stmt.executeQuery("SELECT
ID,NAME,PROGRAMMING_LANGUAGE FROM DEV")
:
```

Regards,
Hiroki

-----Original Message-----
From: glassfish-dev <glassfish-dev-bounces@xxxxxxxxxxx> On Behalf Of
Victor Gimenez via glassfish-dev
Sent: Wednesday, April 24, 2024 3:44 AM
To: glassfish-dev@xxxxxxxxxxx
Cc: Victor Gimenez <victor.gimenez@xxxxxxxxxxxx>
Subject: [glassfish-dev] How to use Glassfish with MySQL or any
database instead of Apache Derby

Hello there,



I am very new to Glassfish and Jakarta EE which I am learning
nowadays, I would like to know what I should do to add MySQL or any
database instead of Apache Derby which is configured as the default
database server? I am very stuck reading the documentation and
without know which files should I configure to add.


Thanks in advance,

--
Victor Borghi Gimenez


Computer Science M.Sc. - Center for Mathematics, Computing and
Cognition
- Federal University of ABC (UFABC)
System Analysis and Development Technologist - São Caetano do Sul
College of Technology (FATEC-SCS)
_______________________________________________
glassfish-dev mailing list
glassfish-dev@xxxxxxxxxxx
To unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/glassfish-dev
_______________________________________________
glassfish-dev mailing list
glassfish-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/glassfish-dev


Back to the top