xconnection.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,398 行 · 第 1/3 页
JAVA
1,398 行
// error. // try { if (null != rs) rs.close(); } catch(Exception e1) { /* Empty */ } try { if (null != stmt) stmt.close(); } catch(Exception e1) { /* Empty */ } try { if (null != con) m_ConnectionPool.releaseConnectionOnError(con); } catch(Exception e1) { /* Empty */ } // Re throw the error so the process can handle the error // normally throw e; } if (DEBUG) System.out.println("..creatingSQLDocument"); DTMManager mgr = ((XPathContext.XPathExpressionContext)exprContext).getDTMManager(); DTMManagerDefault mgrDefault = (DTMManagerDefault) mgr; int dtmIdent = mgrDefault.getFirstFreeDTMID(); SQLDocument doc = new SQLDocument(mgr, dtmIdent << DTMManager.IDENT_DTM_NODE_BITS , m_ConnectionPool, con, stmt, rs, m_IsStreamingEnabled); if (null != doc) { if (DEBUG) System.out.println("..returning Document"); // Register our document mgrDefault.addDTM(doc, dtmIdent); // also keep a local reference m_OpenSQLDocuments.addElement(doc); return doc; } else { // Build Error Doc, BAD Result Set return null; } } catch(SQLException e) { buildErrorDocument(exprContext, e); return null; } catch (Exception e) { buildErrorDocument(exprContext, e); return null; } } /** * Add an untyped value to the parameter list. * @param value * @return */ public void addParameter( String value ) { addParameterWithType(value, null); } /** * Add a typed parameter to the parameter list. * @param value * @param Type * @return */ public void addParameterWithType( String value, String Type ) { m_ParameterList.addElement( new QueryParameter(value, Type) ); } /** * Add a single parameter to the parameter list * formatted as an Element * @param e * @return */ public void addParameterFromElement( Element e ) { NamedNodeMap attrs = e.getAttributes(); Node Type = attrs.getNamedItem("type"); Node n1 = e.getFirstChild(); if (null != n1) { String value = n1.getNodeValue(); if (value == null) value = ""; m_ParameterList.addElement( new QueryParameter(value, Type.getNodeValue()) ); } } /** * Add a section of parameters to the Parameter List * Do each element from the list * @param nl * @return */ public void addParameterFromElement( NodeList nl ) { // // Each child of the NodeList represents a node // match from the select= statment. Process each // of them as a seperate list. // The XML Format is as follows // // <START-TAG> // <TAG1 type="int">value</TAG1> // <TAGA type="int">value</TAGA> // <TAG2 type="string">value</TAG2> // </START-TAG> // // The XSL to process this is formatted as follows // <xsl:param name="plist" select="//START-TAG" /> // <sql:addParameter( $plist ); // int count = nl.getLength(); for (int x=0; x<count; x++) { addParameters( (Element) nl.item(x)); } } /** * @param elem * @return */ private void addParameters( Element elem ) { // // Process all of the Child Elements // The format is as follows // //<TAG type ="typeid">value</TAG> //<TAG1 type ="typeid">value</TAG1> //<TAGA type ="typeid">value</TAGA> // // The name of the Node is not important just is value // and if it contains a type attribute Node n = elem.getFirstChild(); if (null == n) return; do { if (n.getNodeType() == Node.ELEMENT_NODE) { NamedNodeMap attrs = n.getAttributes(); Node Type = attrs.getNamedItem("type"); String TypeStr; if (Type == null) TypeStr = "string"; else TypeStr = Type.getNodeValue(); Node n1 = n.getFirstChild(); if (null != n1) { String value = n1.getNodeValue(); if (value == null) value = ""; m_ParameterList.addElement( new QueryParameter(value, TypeStr) ); } } } while ( (n = n.getNextSibling()) != null); } /** * @return */ public void clearParameters( ) { m_ParameterList.removeAllElements(); } /** * There is a problem with some JDBC drivers when a Connection * is open and the JVM shutsdown. If there is a problem, there * is no way to control the currently open connections in the * pool. So for the default connection pool, the actuall pooling * mechinsm is disabled by default. The Stylesheet designer can * re-enabled pooling to take advantage of connection pools. * The connection pool can even be disabled which will close all * outstanding connections. * @return */ public void enableDefaultConnectionPool( ) { if (DEBUG) System.out.println("Enabling Default Connection Pool"); m_DefaultPoolingEnabled = true; if (m_ConnectionPool == null) return; if (m_IsDefaultPool) return; m_ConnectionPool.setPoolEnabled(true); } /** * See enableDefaultConnectionPool * @return */ public void disableDefaultConnectionPool( ) { if (DEBUG) System.out.println("Disabling Default Connection Pool"); m_DefaultPoolingEnabled = false; if (m_ConnectionPool == null) return; if (!m_IsDefaultPool) return; m_ConnectionPool.setPoolEnabled(false); } /** * Control how the SQL Document uses memory. In Streaming Mode, * memory consumption is greatly reduces so you can have queries * of unlimited size but it will not let you traverse the data * more than once. * @return */ public void enableStreamingMode( ) { if (DEBUG) System.out.println("Enabling Streaming Mode"); m_IsStreamingEnabled = true; } /** * Control how the SQL Document uses memory. In Streaming Mode, * memory consumption is greatly reduces so you can have queries * of unlimited size but it will not let you traverse the data * more than once. * @return */ public void disableStreamingMode( ) { if (DEBUG) System.out.println("Disable Streaming Mode"); m_IsStreamingEnabled = false; } /** * Provide access to the last error that occued. This error * may be over written when the next operation occurs. * @return */ public DTM getError( ) { return m_Error; } /** * Close the connection to the data source. * @return * @throws SQLException */ public void close( )throws SQLException { if (DEBUG) System.out.println("Entering XConnection.close"); // // This function is included just for Legacy support // If it is really called then we must me using a single // document interface, so close all open documents. while(m_OpenSQLDocuments.size() != 0) { SQLDocument d = (SQLDocument) m_OpenSQLDocuments.elementAt(0); d.close(); m_OpenSQLDocuments.removeElementAt(0); } if (DEBUG) System.out.println("Exiting XConnection.close"); } /** * Close the connection to the data source. Only close the connections * for a single document. * @param sqldoc * @return * @throws SQLException */ public void close( SQLDocument sqldoc )throws SQLException { if (DEBUG) System.out.println("Entering XConnection.close"); int size = m_OpenSQLDocuments.size(); for(int x=0; x<size; x++) { SQLDocument d = (SQLDocument) m_OpenSQLDocuments.elementAt(x); if (d == sqldoc) { d.close(); m_OpenSQLDocuments.removeElementAt(x); } } } /** * Set the parameter for a Prepared Statement * @param pos * @param stmt * @param p * @return * @throws SQLException */ public void setParameter( int pos, PreparedStatement stmt, QueryParameter p )throws SQLException { String type = p.getType(); if (type.equalsIgnoreCase("string")) { stmt.setString(pos, p.getValue()); } if (type.equalsIgnoreCase("bigdecimal")) { stmt.setBigDecimal(pos, new BigDecimal(p.getValue())); } if (type.equalsIgnoreCase("boolean")) { Integer i = new Integer( p.getValue() ); boolean b = ((i.intValue() != 0) ? false : true); stmt.setBoolean(pos, b); } if (type.equalsIgnoreCase("bytes")) { stmt.setBytes(pos, p.getValue().getBytes()); } if (type.equalsIgnoreCase("date")) { stmt.setDate(pos, Date.valueOf(p.getValue())); } if (type.equalsIgnoreCase("double")) { Double d = new Double(p.getValue()); stmt.setDouble(pos, d.doubleValue() ); } if (type.equalsIgnoreCase("float")) { Float f = new Float(p.getValue()); stmt.setFloat(pos, f.floatValue()); } if (type.equalsIgnoreCase("long")) { Long l = new Long(p.getValue()); stmt.setLong(pos, l.longValue()); } if (type.equalsIgnoreCase("short")) { Short s = new Short(p.getValue()); stmt.setShort(pos, s.shortValue()); } if (type.equalsIgnoreCase("time")) { stmt.setTime(pos, Time.valueOf(p.getValue()) ); } if (type.equalsIgnoreCase("timestamp")) { stmt.setTimestamp(pos, Timestamp.valueOf(p.getValue()) ); } } /** * @param exprContext * @param excp * @return */ private void buildErrorDocument( ExpressionContext exprContext, SQLException excp ) { try { DTMManager mgr = ((XPathContext.XPathExpressionContext)exprContext).getDTMManager(); DTMManagerDefault mgrDefault = (DTMManagerDefault) mgr; int dtmIdent = mgrDefault.getFirstFreeDTMID(); m_Error = new SQLErrorDocument(mgr, dtmIdent << DTMManager.IDENT_DTM_NODE_BITS, excp); // Register our document mgrDefault.addDTM(m_Error, dtmIdent); } catch(Exception e) { m_Error = null; } } /** * @param exprContext * @param excp * @return */ private void buildErrorDocument( ExpressionContext exprContext, Exception excp ) { try { DTMManager mgr = ((XPathContext.XPathExpressionContext)exprContext).getDTMManager(); DTMManagerDefault mgrDefault = (DTMManagerDefault) mgr; int dtmIdent = mgrDefault.getFirstFreeDTMID(); m_Error = new SQLErrorDocument(mgr, dtmIdent<<DTMManager.IDENT_DTM_NODE_BITS, excp); // Register our document mgrDefault.addDTM(m_Error, dtmIdent); } catch(Exception e) { m_Error = null; } } /** * @return */ protected void finalize( ) { if (DEBUG) System.out.println("In XConnection, finalize"); try { close(); } catch(Exception e) { // Empty We are final Anyway } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?