package org.diagbot.graph.jdbc;

import org.diagbot.pub.utils.PropertiesUtil;
import org.neo4j.driver.v1.*;

import java.sql.Connection;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: fyeman
 * @Date: 2018/9/26/026 14:45
 * @Description:
 */
public class DriverManager {
    public static int ConnLifeTime = 30;
    public static int TransRetryTime = 5;
    public static int ConnPoolSize = 50;
    public static int ConnTimeout = 2;
    public static Driver driver = null;
    public static PropertiesUtil propertiesUtil = new PropertiesUtil("bolt.properties");
    public static Driver newDrive() throws Exception {
        //            driver = GraphDatabase.driver("bolt://192.168.3.112:7687", AuthTokens.basic("neo4j", "123456"),
        if( driver == null){
            driver = GraphDatabase.driver(propertiesUtil.getProperty("bolt.uri"), AuthTokens.basic(propertiesUtil.getProperty("bolt.user"), propertiesUtil.getProperty("bolt.passwd")),
                    Config.build().withMaxConnectionLifetime(ConnLifeTime, TimeUnit.MINUTES)
                            .withMaxTransactionRetryTime(TransRetryTime, TimeUnit.SECONDS)
                            .withMaxConnectionPoolSize(ConnPoolSize)
                            .withConnectionAcquisitionTimeout(ConnTimeout, TimeUnit.MINUTES)
                            .toConfig());
        }
        return driver;
    }

    public static Driver newDrive(String uri, String user, String passwd) throws Exception{
        driver = GraphDatabase.driver("bolt://" + uri, AuthTokens.basic(user, passwd),
                Config.build().withMaxConnectionLifetime(ConnLifeTime, TimeUnit.MINUTES)
                        .withMaxTransactionRetryTime(TransRetryTime, TimeUnit.SECONDS)
                        .withMaxConnectionPoolSize(ConnPoolSize)
                        .withConnectionAcquisitionTimeout(ConnTimeout, TimeUnit.MINUTES)
                        .toConfig());

        return driver;
    }
}
