Search in sources :

Example 1 with DataSourceException

use of org.apache.ibatis.datasource.DataSourceException in project mybatis-3 by mybatis.

the class JndiDataSourceFactoryTest method createJndiDataSource.

private void createJndiDataSource() throws Exception {
    try {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, TEST_INITIAL_CONTEXT_FACTORY);
        MockContext ctx = new MockContext(false);
        ctx.bind(TEST_DATA_SOURCE, expectedDataSource);
        InitialContext initCtx = new InitialContext(env);
        initCtx.bind(TEST_INITIAL_CONTEXT, ctx);
    } catch (NamingException e) {
        throw new DataSourceException("There was an error configuring JndiDataSourceTransactionPool. Cause: " + e, e);
    }
}
Also used : DataSourceException(org.apache.ibatis.datasource.DataSourceException) Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 2 with DataSourceException

use of org.apache.ibatis.datasource.DataSourceException in project mybatis-3 by mybatis.

the class JndiDataSourceFactory method setProperties.

@Override
public void setProperties(Properties properties) {
    try {
        InitialContext initCtx;
        Properties env = getEnvProperties(properties);
        if (env == null) {
            initCtx = new InitialContext();
        } else {
            initCtx = new InitialContext(env);
        }
        if (properties.containsKey(INITIAL_CONTEXT) && properties.containsKey(DATA_SOURCE)) {
            Context ctx = (Context) initCtx.lookup(properties.getProperty(INITIAL_CONTEXT));
            dataSource = (DataSource) ctx.lookup(properties.getProperty(DATA_SOURCE));
        } else if (properties.containsKey(DATA_SOURCE)) {
            dataSource = (DataSource) initCtx.lookup(properties.getProperty(DATA_SOURCE));
        }
    } catch (NamingException e) {
        throw new DataSourceException("There was an error configuring JndiDataSourceTransactionPool. Cause: " + e, e);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) DataSourceException(org.apache.ibatis.datasource.DataSourceException) NamingException(javax.naming.NamingException) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource)

Example 3 with DataSourceException

use of org.apache.ibatis.datasource.DataSourceException in project mybatis-3 by mybatis.

the class UnpooledDataSourceFactory method setProperties.

@Override
public void setProperties(Properties properties) {
    Properties driverProperties = new Properties();
    MetaObject metaDataSource = SystemMetaObject.forObject(dataSource);
    for (Object key : properties.keySet()) {
        String propertyName = (String) key;
        if (propertyName.startsWith(DRIVER_PROPERTY_PREFIX)) {
            String value = properties.getProperty(propertyName);
            driverProperties.setProperty(propertyName.substring(DRIVER_PROPERTY_PREFIX_LENGTH), value);
        } else if (metaDataSource.hasSetter(propertyName)) {
            String value = (String) properties.get(propertyName);
            Object convertedValue = convertValue(metaDataSource, propertyName, value);
            metaDataSource.setValue(propertyName, convertedValue);
        } else {
            throw new DataSourceException("Unknown DataSource property: " + propertyName);
        }
    }
    if (driverProperties.size() > 0) {
        metaDataSource.setValue("driverProperties", driverProperties);
    }
}
Also used : DataSourceException(org.apache.ibatis.datasource.DataSourceException) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) Properties(java.util.Properties)

Aggregations

DataSourceException (org.apache.ibatis.datasource.DataSourceException)3 Properties (java.util.Properties)2 InitialContext (javax.naming.InitialContext)2 NamingException (javax.naming.NamingException)2 Hashtable (java.util.Hashtable)1 Context (javax.naming.Context)1 DataSource (javax.sql.DataSource)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)1