1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
90 }
91 }
92 if (stm != null) {
93 try {
94 stm.close();
95 } catch (final SQLException ignored) {
96
97 }
98 }
99 }
100 }
101 }