Java DataSource initDatasource(String jndiName)

Here you can find the source of initDatasource(String jndiName)

Description

init Datasource

License

Open Source License

Declaration

public static DataSource initDatasource(String jndiName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Main {
    public static DataSource initDatasource(String jndiName) {
        try {// ww w  . j  a va  2s  . c o  m
            javax.naming.Context ctx = new InitialContext();
            // relative to standard JNDI root for J2EE app
            javax.naming.Context envCtx = (javax.naming.Context) ctx.lookup("java:comp/env");
            final DataSource ds = (DataSource) envCtx.lookup(jndiName);
            return new DataSource() {

                @Override
                public Connection getConnection() throws SQLException {
                    Connection conn = ds.getConnection();
                    conn.setAutoCommit(false);
                    return conn;
                }

                @Override
                public Connection getConnection(String username, String password) throws SQLException {
                    Connection conn = ds.getConnection(username, password);
                    conn.setAutoCommit(false);
                    return conn;
                }

                @Override
                public PrintWriter getLogWriter() throws SQLException {
                    return ds.getLogWriter();
                }

                @Override
                public void setLogWriter(PrintWriter out) throws SQLException {
                    ds.setLogWriter(out);
                }

                @Override
                public void setLoginTimeout(int seconds) throws SQLException {
                    ds.setLoginTimeout(seconds);
                }

                @Override
                public int getLoginTimeout() throws SQLException {
                    return ds.getLoginTimeout();
                }

                @Override
                public Logger getParentLogger() throws SQLFeatureNotSupportedException {
                    return ds.getParentLogger();
                }

                @Override
                public <T> T unwrap(Class<T> iface) throws SQLException {
                    return ds.unwrap(iface);
                }

                @Override
                public boolean isWrapperFor(Class<?> iface) throws SQLException {
                    return ds.isWrapperFor(iface);
                }

            };
        } catch (NamingException ex) {
            throw new IllegalStateException(ex);
        }
    }
}

Related

  1. getSchemaSeparator(DataSource dataSource)
  2. getTableColumnCount(DataSource dataSource, String selectedTable)
  3. getTables(DataSource datasource)
  4. hasTable(DataSource ds, String table)
  5. initDataSource()
  6. initDb(DataSource ds)
  7. initializeDataSource()
  8. isDataSourceAvailable(String dataSourceName)
  9. logQueryResultToFile(DataSource dataSource, String path, String sqlStatement, Object... boundVariables)