Example usage for javax.sql DataSource setLogWriter

List of usage examples for javax.sql DataSource setLogWriter

Introduction

In this page you can find the example usage for javax.sql DataSource setLogWriter.

Prototype

@Override
void setLogWriter(java.io.PrintWriter out) throws SQLException;

Source Link

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    // Set up the environment for creating the initial context
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put(Context.PROVIDER_URL, "file:/jdbc");

    Context context = new InitialContext(env);
    NamingEnumeration list = context.list("jdbc");

    while (list.hasMore()) {
        NameClassPair nc = (NameClassPair) list.next();
        System.out.println(nc);/* w  ww.  j a  v  a  2 s  . co m*/
    }

    OracleDataSource ods = new OracleDataSource();
    ods.setDriverType("thin");
    ods.setServerName("localhost");
    ods.setNetworkProtocol("tcp");
    ods.setDatabaseName("databaseName");
    ods.setPortNumber(1521);
    ods.setUser("userName");
    ods.setPassword("Password");

    Context ctx = new InitialContext();
    ctx.bind("file:/jdbc/mydb", ods);

    // Get the initial context of JNDI and lookup the datasource.
    InitialContext ic = new InitialContext();
    javax.sql.DataSource ds = (javax.sql.DataSource) ic.lookup("file:/jdbc/mydb");
    // Set the optional printwriter where the trace log is to be directed.
    ds.setLogWriter(new PrintWriter(new FileOutputStream("c:/datasource.log")));
    Connection con1 = ds.getConnection();
    Connection con2 = ds.getConnection("userName", "password");
    conn.close();

}

From source file:JDBCPool.dbcp.demo.sourcecode.BasicDataSource.java

/**
 * <p>Create (if necessary) and return the internal data source we are using to manage our connections.</p>
 *
 * @throws SQLException if the object pool cannot be created.
 *///www  .  j a v a  2s  .c  o m
protected DataSource createDataSource() throws SQLException {
    if (closed) {
        throw new SQLException("Data source is closed");
    }

    // Return the pool if we have already created it
    // This is double-checked locking. This is safe since dataSource is
    // volatile and the code is targeted at Java 5 onwards.
    if (dataSource != null) {
        return dataSource;
    }
    synchronized (this) {
        if (dataSource != null) {
            return dataSource;
        }

        jmxRegister();

        // create factory which returns raw physical connections
        ConnectionFactory driverConnectionFactory = createConnectionFactory();

        // Set up the poolable connection factory
        boolean success = false;
        PoolableConnectionFactory poolableConnectionFactory;
        try {
            poolableConnectionFactory = createPoolableConnectionFactory(driverConnectionFactory);
            poolableConnectionFactory.setPoolStatements(poolPreparedStatements);
            poolableConnectionFactory.setMaxOpenPrepatedStatements(maxOpenPreparedStatements);
            success = true;
        } catch (SQLException se) {
            throw se;
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception ex) {
            throw new SQLException("Error creating connection factory", ex);
        }

        if (success) {
            // create a pool for our connections
            createConnectionPool(poolableConnectionFactory);
        }

        // Create the pooling data source to manage connections
        DataSource newDataSource;
        success = false;
        try {
            newDataSource = createDataSourceInstance();
            newDataSource.setLogWriter(logWriter);
            success = true;
        } catch (SQLException se) {
            throw se;
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception ex) {
            throw new SQLException("Error creating datasource", ex);
        } finally {
            if (!success) {
                closeConnectionPool();
            }
        }

        // If initialSize > 0, preload the pool
        try {
            for (int i = 0; i < initialSize; i++) {
                connectionPool.addObject(); //?Connection
            }
        } catch (Exception e) {
            closeConnectionPool();
            throw new SQLException("Error preloading the connection pool", e);
        }

        // If timeBetweenEvictionRunsMillis > 0, start the pool's evictor task
        startPoolMaintenance();

        dataSource = newDataSource;
        return dataSource;
    }
}

From source file:com.frameworkset.commons.dbcp2.BasicDataSource.java

/**
 * <p>Create (if necessary) and return the internal data source we are
 * using to manage our connections.</p>
 *
 * @throws SQLException if the object pool cannot be created.
 *//*from  w w w  . ja va 2 s  .c o m*/
protected DataSource createDataSource() throws SQLException {
    if (closed) {
        throw new SQLException("Data source is closed");
    }

    // Return the pool if we have already created it
    // This is double-checked locking. This is safe since dataSource is
    // volatile and the code is targeted at Java 5 onwards.
    if (dataSource != null) {
        return dataSource;
    }
    synchronized (this) {
        if (dataSource != null) {
            return dataSource;
        }

        jmxRegister();

        // create factory which returns raw physical connections
        ConnectionFactory driverConnectionFactory = createConnectionFactory();

        // Set up the poolable connection factory
        boolean success = false;
        PoolableConnectionFactory poolableConnectionFactory;
        try {
            poolableConnectionFactory = createPoolableConnectionFactory(driverConnectionFactory);
            poolableConnectionFactory.setPoolStatements(poolPreparedStatements);
            poolableConnectionFactory.setMaxOpenPrepatedStatements(maxOpenPreparedStatements);
            success = true;
        } catch (SQLException se) {
            throw se;
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception ex) {
            throw new SQLException("Error creating connection factory", ex);
        }

        if (success) {
            // create a pool for our connections
            createConnectionPool(poolableConnectionFactory);
        }

        // Create the pooling data source to manage connections
        DataSource newDataSource;
        success = false;
        try {
            newDataSource = createDataSourceInstance();
            newDataSource.setLogWriter(logWriter);
            success = true;
        } catch (SQLException se) {
            throw se;
        } catch (RuntimeException rte) {
            throw rte;
        } catch (Exception ex) {
            throw new SQLException("Error creating datasource", ex);
        } finally {
            if (!success) {
                closeConnectionPool();
            }
        }

        // If initialSize > 0, preload the pool
        try {
            for (int i = 0; i < initialSize; i++) {
                connectionPool.addObject();
            }
        } catch (Exception e) {
            closeConnectionPool();
            throw new SQLException("Error preloading the connection pool", e);
        }

        // If timeBetweenEvictionRunsMillis > 0, start the pool's evictor task
        startPoolMaintenance();

        dataSource = newDataSource;
        return dataSource;
    }
}