Search in sources :

Example 1 with UnsupportedDatabaseException

use of pro.taskana.exceptions.UnsupportedDatabaseException in project taskana by Taskana.

the class TaskanaEngineImpl method createSqlSessionManager.

/**
 * This method creates the sqlSessionManager of myBatis. It integrates all the SQL mappers and sets the databaseId
 * attribute.
 *
 * @return a {@link SqlSessionFactory}
 */
protected SqlSessionManager createSqlSessionManager() {
    Environment environment = new Environment(DEFAULT, this.transactionFactory, taskanaEngineConfiguration.getDatasource());
    Configuration configuration = new Configuration(environment);
    // set databaseId
    String databaseProductName;
    try (Connection con = taskanaEngineConfiguration.getDatasource().getConnection()) {
        databaseProductName = con.getMetaData().getDatabaseProductName();
        if (databaseProductName.contains("DB2")) {
            configuration.setDatabaseId("db2");
        } else if (databaseProductName.contains("H2")) {
            configuration.setDatabaseId("h2");
        } else {
            LOGGER.error("Method createSqlSessionManager() didn't find database with name {}. Throwing UnsupportedDatabaseException", databaseProductName);
            throw new UnsupportedDatabaseException(databaseProductName);
        }
    } catch (SQLException e) {
        LOGGER.error("Method createSqlSessionManager() could not open a connection to the database. No databaseId has been set.", e);
        throw new SystemException("Method createSqlSessionManager() could not open a connection to the database. No databaseId has been set.");
    }
    // add mappers
    configuration.addMapper(TaskMapper.class);
    configuration.addMapper(TaskMonitorMapper.class);
    configuration.addMapper(WorkbasketMapper.class);
    configuration.addMapper(DistributionTargetMapper.class);
    configuration.addMapper(ClassificationMapper.class);
    configuration.addMapper(WorkbasketAccessMapper.class);
    configuration.addMapper(ObjectReferenceMapper.class);
    configuration.addMapper(QueryMapper.class);
    configuration.addMapper(AttachmentMapper.class);
    configuration.addMapper(JobMapper.class);
    configuration.getTypeHandlerRegistry().register(MapTypeHandler.class);
    SqlSessionFactory localSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    return SqlSessionManager.newInstance(localSessionFactory);
}
Also used : UnsupportedDatabaseException(pro.taskana.exceptions.UnsupportedDatabaseException) TaskanaEngineConfiguration(pro.taskana.configuration.TaskanaEngineConfiguration) Configuration(org.apache.ibatis.session.Configuration) SystemException(pro.taskana.exceptions.SystemException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Environment (org.apache.ibatis.mapping.Environment)1 Configuration (org.apache.ibatis.session.Configuration)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)1 TaskanaEngineConfiguration (pro.taskana.configuration.TaskanaEngineConfiguration)1 SystemException (pro.taskana.exceptions.SystemException)1 UnsupportedDatabaseException (pro.taskana.exceptions.UnsupportedDatabaseException)1