Search in sources :

Example 51 with DataSource

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);
}
Also used : Configuration(org.apache.ibatis.session.Configuration) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) DataSource(javax.sql.DataSource) BeforeClass(org.junit.BeforeClass)

Example 52 with DataSource

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);
    }
}
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 53 with DataSource

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);
}
Also used : SQLException(java.sql.SQLException) CheckResult(zipkin.Component.CheckResult) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 54 with DataSource

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();
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DataAccessException(org.jooq.exception.DataAccessException) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 55 with DataSource

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);
        }
    }
}
Also used : Dao(org.nutz.dao.Dao) ConnCallback(org.nutz.dao.ConnCallback) Connection(java.sql.Connection) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

DataSource (javax.sql.DataSource)546 Connection (java.sql.Connection)200 Test (org.junit.Test)192 SQLException (java.sql.SQLException)118 Context (javax.naming.Context)70 ResultSet (java.sql.ResultSet)59 Statement (java.sql.Statement)59 NamingException (javax.naming.NamingException)57 InitialContext (javax.naming.InitialContext)55 EJBException (javax.ejb.EJBException)40 HashMap (java.util.HashMap)38 PreparedStatement (java.sql.PreparedStatement)37 Properties (java.util.Properties)35 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)34 RemoteException (java.rmi.RemoteException)32 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)31 UserTransaction (javax.transaction.UserTransaction)30 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)26 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21