Java源码示例:org.apache.ibatis.datasource.DataSourceException

示例1
@Test
public void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
  Class<?>[] exceptionTypes = {
      BindingException.class,
      CacheException.class,
      DataSourceException.class,
      ExecutorException.class,
      LogException.class,
      ParsingException.class,
      BuilderException.class,
      PluginException.class,
      ReflectionException.class,
      PersistenceException.class,
      SqlSessionException.class,
      TransactionException.class,
      TypeException.class, 
      ScriptingException.class
  };
  for (Class<?> exceptionType : exceptionTypes) {
    testExceptionConstructors(exceptionType);
  }

}
 
示例2
@Override
public void setProperties(final Properties properties) {
    final Properties driverProperties = new Properties();
    final MetaObject metaDataSource = SystemMetaObject.forObject(dataSource);
    for (final Object key : properties.keySet()) {
        final String propertyName = (String) key;
        if (metaDataSource.hasSetter(propertyName)) {
            final String value = (String) properties.get(propertyName);
            /** 对没有设置值的属性跳过设置 */
            if (StringUtils.isNotEmpty(value) && value.startsWith("${") && value.endsWith("}")) {
                continue;
            }

            final 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);
    }
}
 
示例3
@Test
public void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
  Class<?>[] exceptionTypes = {
      BindingException.class,
      CacheException.class,
      DataSourceException.class,
      ExecutorException.class,
      LogException.class,
      ParsingException.class,
      BuilderException.class,
      PluginException.class,
      ReflectionException.class,
      PersistenceException.class,
      SqlSessionException.class,
      TransactionException.class,
      TypeException.class, 
      ScriptingException.class
  };
  for (Class<?> exceptionType : exceptionTypes) {
    testExceptionConstructors(exceptionType);
  }

}
 
示例4
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);
  }
}
 
示例5
public C3P0DataSourceFactory() {
    try {
        Class<?> comboPooledDataSource = Class.forName("com.mchange.v2.c3p0.ComboPooledDataSource");
        this.dataSource = (DataSource) comboPooledDataSource.newInstance();
    } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new DataSourceException(e.getMessage(), e);
    }
}
 
示例6
public TomcatJdbcPoolDataSourceFactory() {
    try {
        tomcatJdbcDataSource = Class.forName("org.apache.tomcat.jdbc.pool.DataSource");
        this.dataSource = (DataSource) tomcatJdbcDataSource.newInstance();
    } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new DataSourceException(e.getMessage(), e);
    }
}
 
示例7
public DruidDataSourceFactory() {
    try {
        Class<?> druidDataSource = Class.forName("com.alibaba.druid.pool.DruidDataSource");
        this.dataSource = (DataSource) druidDataSource.newInstance();
    } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new DataSourceException(e.getMessage(), e);
    }
}
 
示例8
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);
  }
}
 
示例9
@Override
public void setProperties(final Properties properties) {
    try {
        final Map<String, Object> map = Maps.newHashMap();
        properties.forEach((key, value) -> {
            if (StringUtils.isNotEmpty((String) value) && ((String) value).startsWith("${") && ((String) value).endsWith("}")) {
                return;
            }

            map.put((String) key, value);
        });

        final TomcatJdbcConfig config = TomcatJdbcConfig.mapToBean(map, TomcatJdbcConfig.class);
        if (tomcatJdbcDataSource != null) {
            tomcatJdbcDataSource.getMethod("setDriverClassName", String.class).invoke(dataSource, config.getDriver());
            tomcatJdbcDataSource.getMethod("setUrl", String.class).invoke(dataSource, config.getUrl());
            tomcatJdbcDataSource.getMethod("setUsername", String.class).invoke(dataSource, config.getUserName());
            tomcatJdbcDataSource.getMethod("setPassword", String.class).invoke(dataSource, config.getPasswd());

            if (config.getInitialSize() != null) {
                tomcatJdbcDataSource.getMethod("setInitialSize", int.class).invoke(dataSource, config.getInitialSize());
            }

            if (config.getMinIdle() != null) {
                tomcatJdbcDataSource.getMethod("setMinIdle", int.class).invoke(dataSource, config.getMinIdle());
            }

            if (config.getMaxWait() != null) {
                tomcatJdbcDataSource.getMethod("setMaxWait", int.class).invoke(dataSource, config.getMaxWait());
            }

            if (config.getMaxActive() != null) {
                tomcatJdbcDataSource.getMethod("setMaxActive", int.class).invoke(dataSource, config.getMaxActive());
            }

            if (config.getTestWhileIdle() != null) {
                tomcatJdbcDataSource.getMethod("setTestWhileIdle", boolean.class).invoke(dataSource, config.getTestWhileIdle());
            }

            if (config.getTestOnBorrow() != null) {
                tomcatJdbcDataSource.getMethod("setTestOnBorrow", boolean.class).invoke(dataSource, config.getTestOnBorrow());
            }

            if (config.getValidationInterval() != null) {
                tomcatJdbcDataSource.getMethod("setValidationInterval", long.class).invoke(dataSource, config.getValidationInterval());
            }

            if (config.getTimeBetweenEvictionRunsMillis() != null) {
                tomcatJdbcDataSource.getMethod("setTimeBetweenEvictionRunsMillis", int.class).invoke(dataSource,
                        config.getTimeBetweenEvictionRunsMillis());
            }

            if (config.getLogAbandoned() != null) {
                tomcatJdbcDataSource.getMethod("setLogAbandoned", boolean.class).invoke(dataSource, config.getLogAbandoned());
            }

            if (config.getRemoveAbandoned() != null) {
                tomcatJdbcDataSource.getMethod("setRemoveAbandoned", boolean.class).invoke(dataSource, config.getRemoveAbandoned());
            }

            if (config.getRemoveAbandonedTimeout() != null) {
                tomcatJdbcDataSource.getMethod("setRemoveAbandonedTimeout", int.class).invoke(dataSource, config.getRemoveAbandonedTimeout());
            }

            if (config.getMinEvictableIdleTimeMillis() != null) {
                tomcatJdbcDataSource.getMethod("setMinEvictableIdleTimeMillis", int.class).invoke(dataSource,
                        config.getMinEvictableIdleTimeMillis());
            }

            if (config.getJdbcInterceptors() != null) {
                tomcatJdbcDataSource.getMethod("setJdbcInterceptors", String.class).invoke(dataSource, config.getJdbcInterceptors());
            }

            if (config.getJmxEnabled() != null) {
                tomcatJdbcDataSource.getMethod("setJmxEnabled", boolean.class).invoke(dataSource, config.getJmxEnabled());
            }
        } else {
            throw new DataSourceException("Unknown class [ org.apache.tomcat.jdbc.pool.DataSource ]");
        }
    } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new DataSourceException(e.getMessage(), e);
    }
}