All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.sql.SqlSessionFactory Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.sql;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** A factory class for {@link SqlSession}. */
@ThreadSafe
public class SqlSessionFactory implements AutoCloseable {

  private static final Logger logger = LoggerFactory.getLogger(SqlSessionFactory.class);

  private final SqlConfig config;
  private final SqlTransactionManager sqlTransactionManager;
  @Nullable private final SqlTwoPhaseCommitTransactionManager sqlTwoPhaseCommitTransactionManager;

  private SqlSessionFactory(Properties properties) {
    config = new SqlConfig(properties);
    SqlTransactionFactory sqlTransactionFactory =
        SqlTransactionFactory.builder().withProperties(properties).build();
    sqlTransactionManager = sqlTransactionFactory.createSqlTransactionManager();
    sqlTwoPhaseCommitTransactionManager =
        sqlTransactionFactory.createSqlTwoPhaseCommitTransactionManager();
  }

  public SqlSession createSqlSession() {
    return new SqlSession(
        sqlTransactionManager,
        sqlTwoPhaseCommitTransactionManager,
        config.getDefaultTransactionMode(),
        config.getDefaultNamespaceName().orElse(null),
        config.isDefaultNamespaceNameExistenceCheckEnabled());
  }

  @Override
  public void close() {
    try {
      sqlTransactionManager.close();
    } catch (Exception e) {
      logger.warn("Failed to close sqlTransactionManager", e);
    }
    if (sqlTwoPhaseCommitTransactionManager != null) {
      try {
        sqlTwoPhaseCommitTransactionManager.close();
      } catch (Exception e) {
        logger.warn("Failed to close sqlTwoPhaseCommitTransactionManager", e);
      }
    }
  }

  /**
   * Returns a builder object for {@link SqlSessionFactory}.
   *
   * @return a builder object for {@link SqlSessionFactory}
   */
  public static Builder builder() {
    return new Builder();
  }

  public static class Builder {
    private final Properties properties = new Properties();

    /**
     * Specifies a properties file path.
     *
     * @param path a properties file path
     * @return this object
     */
    public Builder withPropertiesFile(String path) {
      return withPropertiesFile(Paths.get(path));
    }

    /**
     * Specifies a properties file path.
     *
     * @param path a properties file path
     * @return this object
     */
    public Builder withPropertiesFile(Path path) {
      try (InputStream inputStream = Files.newInputStream(path)) {
        properties.load(inputStream);
      } catch (IOException e) {
        throw new UncheckedIOException(e);
      }
      return this;
    }

    /**
     * Adds a property.
     *
     * @param name a property name
     * @param value a property value
     * @return this object
     */
    public Builder withProperty(String name, String value) {
      properties.setProperty(name, value);
      return this;
    }

    /**
     * Adds properties.
     *
     * @param properties properties to add
     * @return this object
     */
    public Builder withProperties(Properties properties) {
      this.properties.putAll(properties);
      return this;
    }

    /**
     * Specifies a default transaction mode.
     *
     * @param defaultTransactionMode a default transaction mode
     * @return this object
     */
    public Builder withDefaultTransactionMode(TransactionMode defaultTransactionMode) {
      properties.setProperty(SqlConfig.DEFAULT_TRANSACTION_MODE, defaultTransactionMode.toString());
      return this;
    }

    /**
     * Specifies a connection mode.
     *
     * @param connectionMode a connection mode
     * @return this object
     */
    public Builder withConnectionMode(String connectionMode) {
      properties.setProperty(SqlConfig.CONNECTION_MODE, connectionMode);
      return this;
    }

    /**
     * Specifies a default namespace name.
     *
     * @param defaultNamespaceName a default namespace name
     * @return this object
     */
    public Builder withDefaultNamespaceName(String defaultNamespaceName) {
      properties.setProperty(SqlConfig.DEFAULT_NAMESPACE_NAME, defaultNamespaceName);
      return this;
    }

    /**
     * Builds a {@link SqlSessionFactory} object with the specified configurations.
     *
     * @return a {@link SqlSessionFactory} object
     */
    public SqlSessionFactory build() {
      return new SqlSessionFactory(properties);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy