You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@submarine.apache.org by pi...@apache.org on 2021/10/29 10:41:15 UTC

[submarine] branch master updated: SUBMARINE-1059. Setup hibernate

This is an automated email from the ASF dual-hosted git repository.

pingsutw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/submarine.git


The following commit(s) were added to refs/heads/master by this push:
     new 81543ed  SUBMARINE-1059. Setup hibernate
81543ed is described below

commit 81543ed7c4bdaafcb3b8f4acf9cc6b6a84a79f37
Author: KUAN-HSUN-LI <b0...@ntu.edu.tw>
AuthorDate: Tue Oct 26 17:37:04 2021 +0800

    SUBMARINE-1059. Setup hibernate
    
    ### What is this PR for?
    * Using an ORM  based database
    Goal:
    * Provide the transaction SQL operation allowing rollback when the operations get wrong.
    
    ### What type of PR is it?
    [Improvement | Feature]
    
    ### Todos
    
    ### What is the Jira issue?
    https://issues.apache.org/jira/browse/SUBMARINE-1059
    
    ### How should this be tested?
    
    ### Screenshots (if appropriate)
    
    ### Questions:
    * Do the license files need updating? No
    * Are there breaking changes for older versions? Yes
    * Does this need new documentation? No
    
    Author: KUAN-HSUN-LI <b0...@ntu.edu.tw>
    
    Signed-off-by: Kevin <pi...@apache.org>
    
    Closes #785 from KUAN-HSUN-LI/SUBMARINE-1059 and squashes the following commits:
    
    f04cd982 [KUAN-HSUN-LI] fix Thread conflict
    721984a1 [KUAN-HSUN-LI] SUBMARINE-1059. setup hibernate
    5c363302 [KUAN-HSUN-LI] SUBMARINE-1059. setup hibernate
    51e88361 [KUAN-HSUN-LI] SUBMARINE-1059. setup hibernate
    beaf63d7 [KUAN-HSUN-LI] SUBMARINE-1059. setup hibernate
---
 pom.xml                                            |  9 ++++
 submarine-server/server-core/pom.xml               | 12 +++++
 .../apache/submarine/server/SubmarineServer.java   |  2 +
 .../server/database/utils/HibernateUtil.java       | 57 ++++++++++++++++++++++
 .../src/main/resources/hibernate.cfg.xml           | 44 +++++++++++++++++
 5 files changed, 124 insertions(+)

diff --git a/pom.xml b/pom.xml
index 3538d01..42a2425 100644
--- a/pom.xml
+++ b/pom.xml
@@ -81,6 +81,9 @@
     <commons-httpclient.version>3.1</commons-httpclient.version>
 
     <cglib.version>3.3.0</cglib.version>
+    <hibernate.version>5.6.0.Final</hibernate.version>
+    <jboss.logging.version>3.4.2.Final</jboss.logging.version>
+    <bytebuddy.version>1.11.20</bytebuddy.version>
     <mybatis.version>3.2.8</mybatis.version>
     <mysql-connector-java.version>5.1.41</mysql-connector-java.version>
     <grpc.version>1.25.0</grpc.version>
@@ -306,6 +309,12 @@
         <artifactId>snakeyaml</artifactId>
         <version>${snakeyaml.version}</version>
       </dependency>
+      <!--  Submarine Server  -->
+      <dependency>
+        <groupId>net.bytebuddy</groupId>
+        <artifactId>byte-buddy</artifactId>
+        <version>${bytebuddy.version}</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 
diff --git a/submarine-server/server-core/pom.xml b/submarine-server/server-core/pom.xml
index 2d80242..53a4183 100644
--- a/submarine-server/server-core/pom.xml
+++ b/submarine-server/server-core/pom.xml
@@ -267,6 +267,18 @@
     </dependency>
 
     <dependency>
+      <groupId>org.hibernate</groupId>
+      <artifactId>hibernate-core</artifactId>
+      <version>${hibernate.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.jboss.logging</groupId>
+          <artifactId>jboss-logging</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>${mysql-connector-java.version}</version>
diff --git a/submarine-server/server-core/src/main/java/org/apache/submarine/server/SubmarineServer.java b/submarine-server/server-core/src/main/java/org/apache/submarine/server/SubmarineServer.java
index 57b71dd..b6be245 100644
--- a/submarine-server/server-core/src/main/java/org/apache/submarine/server/SubmarineServer.java
+++ b/submarine-server/server-core/src/main/java/org/apache/submarine/server/SubmarineServer.java
@@ -19,6 +19,7 @@
 package org.apache.submarine.server;
 
 import org.apache.log4j.PropertyConfigurator;
+import org.apache.submarine.server.database.utils.HibernateUtil;
 import org.apache.submarine.server.rest.provider.YamlEntityProvider;
 import org.apache.submarine.server.workbench.websocket.NotebookServer;
 import org.apache.submarine.commons.cluster.ClusterServer;
@@ -146,6 +147,7 @@ public class SubmarineServer extends ResourceConfig {
                 () -> {
                   LOG.info("Shutting down Submarine Server ... ");
                   try {
+                    HibernateUtil.close();
                     jettyWebServer.stop();
                     Thread.sleep(3000);
                   } catch (Exception e) {
diff --git a/submarine-server/server-core/src/main/java/org/apache/submarine/server/database/utils/HibernateUtil.java b/submarine-server/server-core/src/main/java/org/apache/submarine/server/database/utils/HibernateUtil.java
new file mode 100644
index 0000000..3df8e2f
--- /dev/null
+++ b/submarine-server/server-core/src/main/java/org/apache/submarine/server/database/utils/HibernateUtil.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.submarine.server.database.utils;
+
+
+import org.apache.submarine.commons.runtime.exception.SubmarineRuntimeException;
+import org.hibernate.SessionFactory;
+import org.hibernate.boot.MetadataSources;
+import org.hibernate.boot.registry.StandardServiceRegistry;
+import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HibernateUtil {
+  private static final Logger LOG = LoggerFactory.getLogger(HibernateUtil.class);
+
+  private static final SessionFactory sessionFactory = buildSessionFactory();
+
+  public static void close() {
+    if (sessionFactory != null){
+      sessionFactory.close();
+    }
+    LOG.info("Hibernate session is closed.");
+  }
+
+  public static SessionFactory getSessionFactory() {
+    return sessionFactory;
+  }
+
+  private static SessionFactory buildSessionFactory() throws SubmarineRuntimeException {
+    // Default get the hibernate.cfg.xml in resource
+    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
+    try {
+      return new MetadataSources(registry).buildMetadata().buildSessionFactory();
+    } catch (Exception e) {
+      StandardServiceRegistryBuilder.destroy(registry);
+      LOG.error(e.getMessage(), e);
+      throw new SubmarineRuntimeException("Unable to build session factory");
+    }
+  }
+
+  private HibernateUtil() {}
+}
diff --git a/submarine-server/server-core/src/main/resources/hibernate.cfg.xml b/submarine-server/server-core/src/main/resources/hibernate.cfg.xml
new file mode 100644
index 0000000..0682748
--- /dev/null
+++ b/submarine-server/server-core/src/main/resources/hibernate.cfg.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<!DOCTYPE hibernate-configuration SYSTEM
+  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+  <session-factory>
+    <property name="hibernate.dialect">
+      org.hibernate.dialect.MySQL57Dialect
+    </property>
+    <property name="hibernate.connection.driver_class">
+      com.mysql.jdbc.Driver
+    </property>
+    <property name="hibernate.connection.url">
+      jdbc:mysql://127.0.0.1:3306/submarine?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;zeroDateTimeBehavior=convertToNull&amp;useSSL=false
+    </property>
+    <property name="hibernate.connection.username">
+      submarine
+    </property>
+    <property name="hibernate.connection.password">
+      password
+    </property>
+    <!-- Echo all executed SQL to stdout -->
+    <property name="show_sql">true</property>
+    <!-- Validate schema matches to database -->
+    <property name="hibernate.hbm2ddl.auto">validate</property>
+  </session-factory>
+</hibernate-configuration>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@submarine.apache.org
For additional commands, e-mail: dev-help@submarine.apache.org