View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.common.database.properties;
22  
23  import io.netty.util.internal.PlatformDependent;
24  
25  import java.sql.Connection;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  import java.sql.Statement;
29  
30  import static org.waarp.common.database.model.DbModelMysql.*;
31  
32  /**
33   * MySQL Database Model
34   */
35  public class MySQLProperties implements DbProperties {
36    public static final String PROTOCOL = "mysql";
37  
38    private static final String VALIDATION_QUERY = "select 1";
39  
40    private final String driverName;
41  
42    public MySQLProperties() {
43      if (PlatformDependent.javaVersion() >= 8) {
44        String driver = MYSQL_DRIVER_JRE6;
45        try {
46          Class.forName(MYSQL_DRIVER_JRE8);
47          driver = MYSQL_DRIVER_JRE8;
48        } catch (final Exception e) {
49          driver = MYSQL_DRIVER_JRE6;
50        } finally {
51          driverName = driver;
52        }
53      } else {
54        driverName = MYSQL_DRIVER_JRE6;
55      }
56    }
57  
58    public static String getProtocolID() {
59      return PROTOCOL;
60    }
61  
62    @Override
63    public String getDriverName() {
64      return driverName;
65    }
66  
67    @Override
68    public String getValidationQuery() {
69      return VALIDATION_QUERY;
70    }
71  
72    @Override
73    public int getMaximumConnections(final Connection connection)
74        throws SQLException {
75      Statement stm = null;
76      ResultSet rs = null;
77      try {
78        stm = connection.createStatement();
79        rs = stm.executeQuery("SELECT @@GLOBAL.max_connections");
80        if (!rs.next()) {
81          throw new SQLException("Cannot find max connection");
82        }
83        return rs.getInt(1);
84      } finally {
85        if (rs != null) {
86          try {
87            rs.close();
88          } catch (final SQLException ignored) {
89            // nothing
90          }
91        }
92        if (stm != null) {
93          try {
94            stm.close();
95          } catch (final SQLException ignored) {
96            // nothing
97          }
98        }
99      }
100   }
101 }