Example usage for org.apache.ibatis.datasource.unpooled UnpooledDataSource UnpooledDataSource

List of usage examples for org.apache.ibatis.datasource.unpooled UnpooledDataSource UnpooledDataSource

Introduction

In this page you can find the example usage for org.apache.ibatis.datasource.unpooled UnpooledDataSource UnpooledDataSource.

Prototype

public UnpooledDataSource(String driver, String url, Properties driverProperties) 

Source Link

Usage

From source file:org.fastcatsearch.analytics.db.CommonDBHandler.java

License:Open Source License

public boolean load() {

    if (driverProperties == null) {
        driverProperties = new Properties();
    }/*from  www  .  j a  va 2 s  . c  o m*/

    String dbType = settings.getString("type");
    driverProperties.setProperty("user", settings.getString("user"));
    driverProperties.setProperty("password", settings.getString("password"));
    driverProperties.setProperty("driver.encoding", "UTF-8");

    boolean isAutoCommit = settings.getBoolean("autocommit", true);
    boolean usePooling = settings.getBoolean("usePooling", true);

    DataSource dataSource = null;
    if (usePooling) {
        PooledDataSource pooledDataSource = new PooledDataSource(settings.getString("driver"),
                settings.getString("url"), driverProperties);
        boolean poolPingEnabled = settings.getBoolean("poolPingEnabled", true);
        String poolPingQuery = settings.getString("poolPingQuery");
        int poolPingConnectionsNotUsedFor = settings.getInt("poolPingConnectionsNotUsedFor", -1);
        int poolTimeToWait = settings.getInt("poolTimeToWait", -1);
        int poolMaximumActiveConnections = settings.getInt("poolMaximumActiveConnections", -1);
        int poolMaximumIdleConnections = settings.getInt("poolMaximumIdleConnections", -1);

        pooledDataSource.setPoolPingEnabled(poolPingEnabled);

        if (poolPingQuery != null) {
            pooledDataSource.setPoolPingQuery(poolPingQuery);
        }
        if (poolPingConnectionsNotUsedFor != -1) {
            pooledDataSource.setPoolPingConnectionsNotUsedFor(poolPingConnectionsNotUsedFor);
        }
        if (poolTimeToWait != -1) {
            pooledDataSource.setPoolTimeToWait(poolTimeToWait);
        }
        if (poolMaximumActiveConnections != -1) {
            pooledDataSource.setPoolMaximumActiveConnections(poolMaximumActiveConnections);
        }
        if (poolMaximumIdleConnections != -1) {
            pooledDataSource.setPoolMaximumIdleConnections(poolMaximumIdleConnections);
        }
        //autocommit
        pooledDataSource.setDefaultAutoCommit(isAutoCommit);

        dataSource = pooledDataSource;

    } else {
        UnpooledDataSource unpooledDataSource = new UnpooledDataSource(settings.getString("driver"),
                settings.getString("url"), driverProperties);
        unpooledDataSource.setAutoCommit(isAutoCommit);
        dataSource = unpooledDataSource;
    }

    org.apache.ibatis.mapping.Environment environment = new org.apache.ibatis.mapping.Environment("ID",
            new JdbcTransactionFactory(), dataSource);
    Configuration configuration = new Configuration(environment);
    if (globalParam != null) {
        configuration.getVariables().putAll(globalParam);
    }

    if (mapperList != null) {
        List<URL> mapperFileList = new ArrayList<URL>();
        for (Class<?> mapperDAO : mapperList) {
            try {
                String mapperFilePath = mapperDAO.getName().replace('.', '/') + "_" + dbType + ".xml";
                URL mapperFile = Resources.getResourceURL(mapperFilePath);
                mapperFileList.add(mapperFile);
            } catch (IOException e) {
                logger.error("error load MapperFile", e);
            }
        }

        for (URL mapperFile : mapperFileList) {
            addSqlMappings(configuration, mapperFile);
        }
    }

    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    return true;
}

From source file:org.neo4j.jdbc.example.mybatis.MybatisTest.java

License:Apache License

protected void buildMybatisConfiguration(String protocol, String host, int port) {
    DataSource dataSource = new UnpooledDataSource("org.neo4j.jdbc.Driver",
            "jdbc:neo4j:" + protocol + "://" + host + ":" + port + "?noSsl", null);
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);

    Configuration configuration = new Configuration(environment);
    configuration.getMapperRegistry().addMapper(ActorMapper.class);
    configuration.addLoadedResource("org/neo4j/jdbc/example/mybatis/mapper/ActorMapper.xml");

    ConnectionFactory.getSqlSessionFactory(configuration);
}