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
  • From: "Hiroki Sawamura (Fujitsu)" <sawamura.hiroki@xxxxxxxxxxx>
  • Date: Thu, 25 Apr 2024 08:33:08 +0000
  • Accept-language: ja-JP, en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fujitsu.com; dmarc=pass action=none header.from=fujitsu.com; dkim=pass header.d=fujitsu.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QAtkRzprxtXC26QbOIBi4+QaogDuh8pokwUF0xv7ywc=; b=V9ZG9aM82oHPB/j701OtmBjqm3WZytXtBqBrvJQ/M6fVyghMGM3uxLl5V1AIwZTJdV8KfcRHiP/uPPdl3bUmYD+R/b8uAQRG6VWg+YDOHmP5LBy+SlKsk5U5QM2ZxNLwpW/aGKgBZCZ/M3nsIAUD1tDfyAHbUXOzwatsiHAtP/dBeMtZ+YuWvpZ0LCEHq1dtimUw6M9VyJH45YN5v7u0q+EQCfMyn7zN9m2jQQ42VthJgV3ADBQ52OlEfjCFmAQx/WHVLX47KJ+DHt2QCpS4cRMvyjocS1fiWoS8lzc7uNNI+LoaNrY26RECR6XhF5gHHegczSoa2EbvE/GCI/sW1g==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NpZbxtsRXmBLp/V40mkwUaTrlTK+mMmlvRRRRDhO4W1yqEsTgTNJDmy6lEmfzy4NdXLdr6q4jkMaGKOp2sGFUfewZJ35i46+SUdSh1ZUhzIbdInWtLNstnHoDvIAr3pLCHGiqGub6pKcTe3xKio7udAv5MnsseBHYvRfzLqpD2pZjzDtpqSmq1o/uiS0R+Jd96XXLqYhinYWrMJwAtBACZ1z94WRTAZ0cojiawjggCfY0q+dVEi2D1Uml6KnKyYEOYYmJs32UjTYAU66vOSwaUH1Eb7WvxmIDQLss3mW2vB6eLKG6IgCbukvBfgxQxpeuwtpTguuIvrARsnvz0uYQw==
  • Delivered-to: glassfish-dev@xxxxxxxxxxx
  • List-archive: <https://www.eclipse.org/mailman/private/glassfish-dev/>
  • List-help: <mailto:glassfish-dev-request@eclipse.org?subject=help>
  • List-subscribe: <https://www.eclipse.org/mailman/listinfo/glassfish-dev>, <mailto:glassfish-dev-request@eclipse.org?subject=subscribe>
  • List-unsubscribe: <https://www.eclipse.org/mailman/options/glassfish-dev>, <mailto:glassfish-dev-request@eclipse.org?subject=unsubscribe>
  • Msip_labels: MSIP_Label_a7295cc1-d279-42ac-ab4d-3b0f4fece050_ActionId=33bdcee1-1ed8-4f5e-8c63-48eb5a28ffa6;MSIP_Label_a7295cc1-d279-42ac-ab4d-3b0f4fece050_ContentBits=0;MSIP_Label_a7295cc1-d279-42ac-ab4d-3b0f4fece050_Enabled=true;MSIP_Label_a7295cc1-d279-42ac-ab4d-3b0f4fece050_Method=Standard;MSIP_Label_a7295cc1-d279-42ac-ab4d-3b0f4fece050_Name=FUJITSU-RESTRICTED​;MSIP_Label_a7295cc1-d279-42ac-ab4d-3b0f4fece050_SetDate=2024-04-25T08:04:52Z;MSIP_Label_a7295cc1-d279-42ac-ab4d-3b0f4fece050_SiteId=a19f121d-81e1-4858-a9d8-736e267fd4c7;
  • Thread-index: AQHala5wbjXaEz077kKB65ivaP4PxbF26ttAgADXxQCAAGXFoIAAdDQAgAAGixA=
  • Thread-topic: [glassfish-dev] How to use Glassfish with MySQL or any database instead of Apache Derby

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".

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;
```

-----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

--
Victor Borghi Gimenez


Computer Science M.Sc. - Center for Mathematics, Computing and Cognition 
- Federal University of ABC (UFABC) https://www.ufabc.edu.br/en/
System Analysis and Development Technologist - São Caetano do Sul 
College of Technology (FATEC-SCS) https://www.fatecsaocaetano.edu.br/

http://lattes.cnpq.br/4344720857265863
https://www.linkedin.com/in/victor-borghi-giménez-04466666/
https://github.com/VictorGimenez

Back to the top