use of javax.sql.DataSource in project mybatis-3 by mybatis.
the class ClobReaderTypeHandlerTest method setupSqlSessionFactory.
@BeforeClass
public static void setupSqlSessionFactory() throws Exception {
DataSource dataSource = BaseDataTest.createUnpooledDataSource("org/apache/ibatis/type/jdbc.properties");
BaseDataTest.runScript(dataSource, "org/apache/ibatis/type/ClobReaderTypeHandlerTest.sql");
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("Production", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(Mapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
use of javax.sql.DataSource 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);
}
}
use of javax.sql.DataSource in project zipkin by openzipkin.
the class MySQLStorageTest method check_failsInsteadOfThrowing.
@Test
public void check_failsInsteadOfThrowing() throws SQLException {
DataSource dataSource = mock(DataSource.class);
when(dataSource.getConnection()).thenThrow(new SQLException("foo"));
CheckResult result = storage(dataSource).check();
assertThat(result.ok).isFalse();
assertThat(result.exception).isInstanceOf(SQLException.class);
}
use of javax.sql.DataSource in project zipkin by openzipkin.
the class SchemaTest method hasDependencies_missing.
@Test
public void hasDependencies_missing() throws SQLException {
SQLSyntaxErrorException sqlException = new SQLSyntaxErrorException("SQL [select count(*) from `zipkin_dependencies`]; Table 'zipkin.zipkin_dependencies' doesn't exist\n" + " Query is : select count(*) from `zipkin_dependencies`", "42S02", 1146);
DataSource dataSource = mock(DataSource.class);
// cheats to lower mock count: this exception is really thrown during execution of the query
when(dataSource.getConnection()).thenThrow(new DataAccessException(sqlException.getMessage(), sqlException));
assertThat(schema.hasPreAggregatedDependencies).isFalse();
}
use of javax.sql.DataSource in project nutz by nutzam.
the class DaoUpTest method test_connection.
/**
* 操作数据库连接
* @throws SQLException
*/
@Test
public void test_connection() throws SQLException {
// 有2种方式,看你喜欢
// 第一种, 在Dao接口下执行
Dao dao = DaoUp.me().dao();
dao.run(new ConnCallback() {
public void invoke(Connection conn) throws Exception {
// 做任何你想做的jdbc操作,但最好别关闭这个conn, 因为nutz会为你处理好
// 如果当前上下文是事务,那这个连接就是事务那个连接
}
});
// 第二种,不经过Dao,直接从DataSource. 如果是Mvc应用,请通过注入获取DataSource
DataSource ds = DaoUp.me().getDataSource();
Connection conn = null;
try {
conn = ds.getConnection();
// 做爱做的事吧 ^_^
} finally {
try {
if (conn != null)
// 务必关闭连接!!!
conn.close();
} catch (Throwable e) {
log.debug("fail to close Connection", e);
}
}
}
Aggregations