List of usage examples for javax.sql DataSource getConnection
Connection getConnection() throws SQLException;
Attempts to establish a connection with the data source that this DataSource object represents.
From source file:org.apache.ode.bpel.extvar.jdbc.JdbcExternalVariableModule.java
public void configure(QName pid, String extVarId, Element config) throws ExternalVariableModuleException { EVarId evarId = new EVarId(pid, extVarId); DataSource ds = null; Element jndiDs = DOMUtils.findChildByName(config, new QName(JDBC_NS, "datasource-jndi")); Element jndiRef = DOMUtils.findChildByName(config, new QName(JDBC_NS, "datasource-ref")); Element initMode = DOMUtils.findChildByName(config, new QName(JDBC_NS, "init-mode")); if (jndiRef != null) { String refname = jndiRef.getTextContent().trim(); ds = _dataSources.get(refname);/* w w w. j a v a 2 s . c o m*/ if (ds == null) throw new ExternalVariableModuleException( "Data source reference \"" + refname + "\" not found for external variable " + evarId + "; make sure to register the data source with the engine!"); } else if (jndiDs != null) { String name = jndiDs.getTextContent().trim(); Object dsCandidate; InitialContext ctx; try { ctx = new InitialContext(); } catch (Exception ex) { throw new ExternalVariableModuleException( "Unable to access JNDI context for external variable " + evarId, ex); } try { dsCandidate = ctx.lookup(name); } catch (Exception ex) { throw new ExternalVariableModuleException("Lookup of data source for " + evarId + " failed.", ex); } finally { try { ctx.close(); } catch (NamingException e) { /* ignore */ } } if (dsCandidate == null) throw new ExternalVariableModuleException("Data source \"" + name + "\" not found in JNDI!"); if (!(dsCandidate instanceof DataSource)) throw new ExternalVariableModuleException( "JNDI object \"" + name + "\" does not implement javax.sql.DataSource"); ds = (DataSource) dsCandidate; } if (ds == null) { throw new ExternalVariableModuleException( "No valid data source configuration for JDBC external varible " + evarId); } Connection conn = null; DatabaseMetaData metaData; try { conn = ds.getConnection(); metaData = conn.getMetaData(); } catch (Exception ex) { try { if (conn != null) conn.close(); } catch (SQLException e) { // ignore } throw new ExternalVariableModuleException( "Unable to open database connection for external variable " + evarId, ex); } try { DbExternalVariable dbev = new DbExternalVariable(evarId, ds); if (initMode != null) try { dbev._initType = InitType.valueOf(initMode.getTextContent().trim()); } catch (Exception ex) { throw new ExternalVariableModuleException( "Invalid <init-mode> value: " + initMode.getTextContent().trim()); } Element tableName = DOMUtils.findChildByName(config, new QName(JDBC_NS, "table")); if (tableName == null || tableName.getTextContent().trim().equals("")) throw new ExternalVariableModuleException("Must specify <table> for external variable " + evarId); String table = tableName.getTextContent().trim(); String schema = null; if (table.indexOf('.') != -1) { schema = table.substring(0, table.indexOf('.')); table = table.substring(table.indexOf('.') + 1); } if (metaData.storesLowerCaseIdentifiers()) { table = table.toLowerCase(); if (schema != null) schema = table.toLowerCase(); } else if (metaData.storesUpperCaseIdentifiers()) { table = table.toUpperCase(); if (schema != null) schema = schema.toUpperCase(); } dbev.generatedKeys = metaData.supportsGetGeneratedKeys(); ResultSet tables = metaData.getTables(null, schema, table, null); if (tables.next()) { dbev.table = tables.getString("TABLE_NAME"); dbev.schema = tables.getString("TABLE_SCHEM"); } else throw new ExternalVariableModuleException("Table \"" + table + "\" not found in database."); tables.close(); List<Element> columns = DOMUtils.findChildrenByName(config, new QName(JDBC_NS, "column")); for (Element col : columns) { String name = col.getAttribute("name"); String colname = col.getAttribute("column-name"); String key = col.getAttribute("key"); String gentype = col.getAttribute("generator"); String expression = col.getAttribute("expression"); if (key == null || "".equals(key)) key = "no"; if (gentype == null || "".equals(gentype)) gentype = GenType.none.toString(); if (colname == null || "".equals(colname)) colname = name; if (name == null || "".equals(name)) throw new ExternalVariableModuleException( "External variable " + evarId + " <column> element must have \"name\" attribute. "); if (metaData.storesLowerCaseIdentifiers()) colname = colname.toLowerCase(); else if (metaData.storesUpperCaseIdentifiers()) colname = colname.toUpperCase(); GenType gtype; try { gtype = GenType.valueOf(gentype); } catch (Exception ex) { throw new ExternalVariableModuleException("External variable " + evarId + " column \"" + name + "\" generator type \"" + gentype + "\" is unknown."); } if (gtype == GenType.expression && (expression == null || "".equals(expression))) throw new ExternalVariableModuleException("External variable " + evarId + " column \"" + name + "\" used \"expression\" generator, but did not specify an expression"); Column c = dbev.new Column(name, colname, key.equalsIgnoreCase("yes"), gtype, expression); ResultSet cmd = metaData.getColumns(null, dbev.schema, dbev.table, colname); try { if (cmd.next()) { c.dataType = cmd.getInt("DATA_TYPE"); c.nullok = cmd.getInt("NULLABLE") != 0; } else throw new ExternalVariableModuleException("External variable " + evarId + " referenced " + "non-existant column \"" + colname + "\"!"); } finally { cmd.close(); } dbev.addColumn(c); } if (dbev.numColumns() == 0) throw new ExternalVariableModuleException( "External variable " + evarId + " did not have any <column> elements!"); _vars.put(evarId, dbev); } catch (SQLException se) { throw new ExternalVariableModuleException("SQL Error", se); } finally { try { conn.close(); } catch (SQLException e) { } } }
From source file:org.apache.hadoop.hive.metastore.txn.TxnHandler.java
private Connection getDbConn(int isolationLevel, DataSource connPool) throws SQLException { int rc = doRetryOnConnPool ? 10 : 1; Connection dbConn = null;/*from w ww .j a va2s .c o m*/ while (true) { try { dbConn = connPool.getConnection(); dbConn.setAutoCommit(false); dbConn.setTransactionIsolation(isolationLevel); return dbConn; } catch (SQLException e) { closeDbConn(dbConn); if ((--rc) <= 0) throw e; LOG.error("There is a problem with a connection from the pool, retrying(rc=" + rc + "): " + getMessage(e), e); } } }
From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java
/** * Create a connection from the pool. This is not part of the constructor to * allow the method to have return codes that can be interrogated by the * caller. If Exception are desired, appropriate wrapper methods can be * created to provide both features and give the caller the flexibility to * use either without additional coding. * <p>/* w ww. j a va2 s. co m*/ * Be sure to call DBAlert.close() to complete the request before returning * to the client or loosing the object focus in the caller to "new * DBAlert()". * * @param ds_ * The datasource for database connections. * @param user_ * The database user logon id. * @return 0 if successful, otherwise the error code. * @see gov.nih.nci.cadsr.sentinel.database.DBAlert#close close() */ public int open(DataSource ds_, String user_) { try { _user = user_; _conn = ds_.getConnection(); _conn.setAutoCommit(false); _needCommit = false; } catch (SQLException ex) { _logger.error(ex.toString(), ex); return ex.getErrorCode(); } return 0; }
From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java
/** * Get the Sentinel Help properties from the tool options table. * //w ww.ja va2 s . c o m * @param ds_ the datasource for the connection * @return the map of property values */ public HashMap<String, String> getHelpProps(DataSource ds_) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; HashMap<String, String> props = new HashMap<String, String>(); try { conn = ds_.getConnection(); pstmt = conn.prepareStatement( "select property, value from sbrext.tool_options_view_ext where tool_name = 'SENTINEL' and property like 'HELP.%' "); rs = pstmt.executeQuery(); while (rs.next()) { props.put(rs.getString(1), rs.getString(2)); } } catch (Exception ex) { // Don't care, this is non-critical, continue processing. } finally { if (rs != null) { try { rs.close(); } catch (Exception ex) { } } if (pstmt != null) { try { pstmt.close(); } catch (Exception ex) { } } if (conn != null) { try { conn.close(); } catch (Exception ex) { } } } return props; }
From source file:com.portfolio.data.provider.MysqlAdminProvider.java
@Override public void connect(Properties connectionProperties) throws Exception { InitialContext cxt = new InitialContext(); if (cxt == null) { throw new Exception("no context found!"); }// w w w . j av a 2 s . c om DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/portfolio-backend"); if (ds == null) { throw new Exception("Data jdbc/portfolio-backend source not found!"); } connection = ds.getConnection(); }