2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.jdbc.internal.db;
15 import java.util.Objects;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.knowm.yank.Yank;
20 import org.knowm.yank.exceptions.YankSQLException;
21 import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
22 import org.openhab.persistence.jdbc.internal.utils.DbMetaData;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Extended Database Configuration class. Class represents
28 * the extended database-specific configuration. Overrides and supplements the
29 * default settings from JdbcBaseDAO. Enter only the differences to JdbcBaseDAO here.
31 * since driver version >= 6.0 sometimes timezone conversation is needed: ?serverTimezone=UTC
32 * example: dbProps.setProperty("jdbcUrl", "jdbc:mysql://192.168.0.181:3306/ItemTypeTest3?serverTimezone=UTC");//mysql
35 * @author Helmut Lehmeyer - Initial contribution
38 public class JdbcMysqlDAO extends JdbcBaseDAO {
39 private static final String DRIVER_CLASS_NAME = com.mysql.cj.jdbc.Driver.class.getName();
40 @SuppressWarnings("unused")
41 private static final String DATA_SOURCE_CLASS_NAME = com.mysql.cj.jdbc.MysqlDataSource.class.getName();
43 private final Logger logger = LoggerFactory.getLogger(JdbcMysqlDAO.class);
48 public JdbcMysqlDAO() {
54 private void initSqlQueries() {
55 logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
59 * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
61 private void initSqlTypes() {
62 logger.debug("JDBC::initSqlTypes: Initialize the type array");
63 sqlTypes.put("STRINGITEM", "VARCHAR(21717)");// mysql using utf-8 max 65535/3 = 21845, using 21845-128 = 21717
67 * INFO: https://github.com/brettwooldridge/HikariCP
69 private void initDbProps() {
71 databaseProps.setProperty("dataSource.cachePrepStmts", "true");
72 databaseProps.setProperty("dataSource.prepStmtCacheSize", "250");
73 databaseProps.setProperty("dataSource.prepStmtCacheSqlLimit", "2048");
74 databaseProps.setProperty("dataSource.jdbcCompliantTruncation", "false");// jdbc standard max varchar max length
77 // Properties for HikariCP
78 // Use driverClassName
79 databaseProps.setProperty("driverClassName", DRIVER_CLASS_NAME);
80 // OR dataSourceClassName
81 // databaseProps.setProperty("dataSourceClassName", DATA_SOURCE_CLASS_NAME);
82 databaseProps.setProperty("maximumPoolSize", "3");
83 databaseProps.setProperty("minimumIdle", "2");
87 public void initAfterFirstDbConnection() {
88 logger.debug("JDBC::initAfterFirstDbConnection: Initializing step, after db is connected.");
89 DbMetaData dbMeta = new DbMetaData();
91 // Initialize sqlTypes, depending on DB version for example
92 if (dbMeta.isDbVersionGreater(5, 5)) {
93 sqlTypes.put("DATETIMEITEM", "DATETIME(3)");
94 sqlTypes.put("tablePrimaryKey", "TIMESTAMP(3)");
95 sqlTypes.put("tablePrimaryValue", "NOW(3)");
103 public @Nullable Integer doPingDB() throws JdbcSQLException {
105 final @Nullable Long result = Yank.queryScalar(sqlPingDB, Long.class, null);
106 return Objects.nonNull(result) ? result.intValue() : null;
107 } catch (YankSQLException e) {
108 throw new JdbcSQLException(e);
116 /****************************
117 * SQL generation Providers *
118 ****************************/
124 /******************************
125 * public Getters and Setters *
126 ******************************/