In OracleJSP, the connection beans support the API getConnection() that returns JDBC connections. Specifically, there are two connection beans in OracleJSP:
oracle.jsp.dbutil.ConnBean,SQLJ-specific connection beans extends the original connection beans with APIs that returns SQLJ connection contexts in OracleJSP applications. Specifically, there are two SQLJ-specific connection beans:
oracle.jsp.dbutil.ConnCacheBean.
oracle.sqlj.runtime.SqljConnBean,
oracle.sqlj.runtime.SqljConnCacheBean.
Both SQLJ-specific connection beans support a new bean property ContextClass of Type java.lang.Class and the following APIs:
public synchronized void setContextClass(Class contextClass)The bean property ContextClass indicates type of the connection context returned by getContext() method call. By default, the value of the bean property ContextClass is sqlj.runtime.ref.DefaultContext. The setContextClass() and getContextClass() methods are setter and getter of that bean property.
public synchronized Class getContextClass()
public synchronized sqlj.runtime.ref.DefaultContext getDefaultContext()
public synchronized sqlj.runtime.ConnectionContext getContext().
The getDefaultContext() and getContext() methods are handled
differently for SqljConnBean and SqljConnCacheBean. Below,
we discuss the handling of the two methods for SqljConnBean and
SqljConnCacheBean
respectively.
The getDefaultContext() method returns a DefaultContext instance. The getContext() methods returns a connection context of the type specified by the ContextClass bean property.
The first getDefaultContext() or getContext() method call will create and return a connection context based on the JDBC connection associated with the connection bean. The returned connection context will also be stored in the SqljConnBean instance.
Once a connection context has been created and stored, the behaviour of subsequent getDefaultContext() method calls will depend on the type of the stored connection context. If the stored connection context is a DefaultContext instance, getDefaultContext() will return the stored connection context. If the stored connection context is not a DefaultContext instance, getDefaultContext() will close the stored connection context, reuse the JDBC connection associated with that stored connection context to create and return a new connection context.
Once a connection context has been created and stored, the behaviour of subsequent getContext() method calls will depend on the type of the stored connection context and the current setting of the ContextClass bean property. If the stored connection context is of the same type as specified by ContextClass, getContext() will return the stored connection context. If the stored connection context is not of the type specified by ContextClass, getContext() will close the stored connection context, reuse the JDBC connection associated with the stored connection context to create and return a new connection context.
A newly created connection context is stored to replace the previously stored connection context.
As mentioned before, at one time, only one active connection context is allowd for a SqljConnBean instance. Closing the logical JDBC connection instantiated by a SqljConnBean instance will force any active connection context in that to SqljConnBean instance be shutdown. Closing a connection context using close() or close(ConnectionContext.CLOSE_CONNECTION) will shutdown the associated JDBC connection, while closing a connection context using close(ConnectionContext.KEEP_CONNECTION) will keep the associated JDBC connection active.
Closing a logical JDBC connection created by a SqljConnCacheBean instance will shutdown the connection context associated with that JDBC connection. Closing a connection context using close() or close(ConnectionContext.CLOSE_CONNECTION) will shutdown the associated JDBC connection, while closing a connection context using close(ConnectionContext.KEEP_CONNECTION) will keep the associated JDBC connection active.
<%@ page language="sqlj"
import="java.sql.*, oracle.sqlj.runtime.SqljConnCacheBean" %><jsp:useBean id="cbean" class="oracle.sqlj.runtime.SqljConnCacheBean" scope="session">
<jsp:setProperty name="cbean" property="User" value="scott"/>
<jsp:setProperty name="cbean" property="Password" value="tiger"/>
<jsp:setProperty name="cbean" property="URL" value="jdbc:oracle:thin:@pdcsun-dev3:1521:view13"/>
<jsp:setProperty name="cbean" property="ContextClass" value="sqlj.runtime.ref.DefaultContext"/>
</jsp:useBean><HTML>
<HEAD> <TITLE> The SQLJSelectInto JSP </TITLE> </HEAD>
<BODY BGCOLOR=white><% String empno = request.getParameter("empno");
if (empno != null) { %>
<H3> Employee # <%=empno %> Details: </H3>
<% String ename = null; double sal = 0.0; String hireDate = null;
StringBuffer sb = new StringBuffer();
sqlj.runtime.ref.DefaultContext ctx=null;
try {
// Make the Connection
ctx = (sqlj.runtime.ref.DefaultContext) cbean.getContext();
} catch (SQLException e) {
}
try {
#sql [ctx] { SELECT ename, sal, TO_CHAR(hiredate, 'DD-MON-YYYY')
INTO :ename, :sal, :hireDate
FROM scott.emp WHERE UPPER(empno) = UPPER(:empno)
};
sb.append("<BLOCKQUOTE><BIG><B><PRE>\n");
sb.append("Name : " + ename + "\n");
sb.append("Salary : " + sal + "\n");
sb.append("Date hired : " + hireDate);
sb.append("</PRE></B></BIG></BLOCKQUOTE>");} catch (java.sql.SQLException e) {
sb.append("<P> SQL error: <PRE> " + e + " </PRE> </P>\n");
} finally {
if (ctx!= null) ctx.close();
}
%>
<H3><%=sb.toString()%></H3>
<%}
%><B>Enter an employee number:</B>
<FORM METHOD=get>
<INPUT TYPE="text" NAME="empno" SIZE=10>
<INPUT TYPE="submit" VALUE="Ask Oracle");
</FORM>
</BODY>
</HTML>
<%
%>