/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import javax.transaction.*;
/**
*
* @author zhaojy
*/
public class Test extends HttpServlet {
/**
* Processes requests for both HTTP GET
and POST
methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
static Connection connectin = null;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text;charset=UTF-8");
PrintWriter out = response.getWriter();
UserTransaction ut = null;
try {
// Connection connectin = null;
try{
ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
ut.begin();
out.println( "================In this test, you should set LazyAssociation to true first.===================");
if(connectin == null){
connectin = ((DataSource)new InitialContext().lookup("jdbc/sample")).getConnection();
out.println( "get Connection from jdbc/sample.");
}else{
out.println("connection already acquired : " + connectin);
}
connectin.createStatement();//to make sure that the connection is used and hence lazy-assoc can happen
debug(out);
/* Class ConnectionHolder = connectin.getClass().getClassLoader().loadClass("com.sun.gjc.spi.base.ConnectionHolder");
out.println( "do nothing but get the managed connection and physical connection.");
out.println( "Managed Connection: " + ConnectionHolder.getDeclaredMethod("getManagedConnection").invoke(connectin) );
out.println( "Actual Connection: " + ConnectionHolder.getDeclaredMethod("getConnection").invoke(connectin) );*/
ut.commit();
} catch (Exception exception) {
exception.printStackTrace(out);
if(ut != null){
try{
ut.rollback();
}catch(Exception e){
e.printStackTrace();
}
}
} finally{
/* try {
if (connectin != null && !connectin.isClosed()) {
connectin.close();
}
} catch (SQLException ex) {
ex.printStackTrace(out);
}*/
}
} finally {
out.close();
}
}
private void debug(PrintWriter out) throws Exception{
if(connectin != null){
Class ConnectionHolder = connectin.getClass().getClassLoader().loadClass("com.sun.gjc.spi.base.ConnectionHolder");
out.println( "do nothing but get the managed connection and physical connection.");
out.println( "Managed Connection: " + ConnectionHolder.getDeclaredMethod("getManagedConnection").invoke(connectin) );
out.println( "Actual Connection: " + ConnectionHolder.getDeclaredMethod("getConnection").invoke(connectin) );
}else{
out.println("connectin is null");
}
}
//
/**
* Handles the HTTP GET
method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}