Java JDBC MySQL Connection checkConnection(String dbFile)

Here you can find the source of checkConnection(String dbFile)

Description

check Connection

License

Open Source License

Declaration

private static void checkConnection(String dbFile) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private final static Map<String, Connection> connections = new HashMap<>();

    private static void checkConnection(String dbFile) {
        try {//from w w w  .jav  a2 s  .  com
            Connection connection = connections.get(dbFile);

            if (connection.isClosed())
                initializeConnection(dbFile);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public synchronized static void initializeConnection(String dbFile) {
        try {
            Connection connection = DriverManager.getConnection(getDbURL(dbFile));

            connections.put(dbFile, connection);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

    }

    public static Connection getConnection(String dbFile) {
        checkConnection(dbFile);
        return connections.get(dbFile);
    }

    public static String getDbURL(String dbFile) {
        return "jdbc:h2:" + dbFile + ";MODE=MySQL;FILE_LOCK=NO";
    }
}

Related

  1. addItem(int itemId, String itemName, int page, String tradeable)
  2. computeAverageGeneIdsPerName()
  3. createConnection()
  4. createConnection(String hostname, String port, String database, String username, String password)
  5. createConnection(String url, String user, String password)