Friday, May 29, 2009

JSP to test transactions

<%@ page import="java.sql.*, javax.sql.*, javax.naming.*, java.io.*, java.util.*, javax.transaction.*" %>



<%
System.out.println("hello this is testTx.jsp");
String dsName = "wlsbjmsrpDataSource";

Hashtable<String, String> h = new Hashtable<String, String>();
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");


Context initialContext = new InitialContext(h);

UserTransaction ut = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
System.out.println("beginning transaction");

ut.begin();
System.out.println("transaction is " + ut.toString());


//UserTransaction utx = (UserTransaction)initialContext.lookup("UserTransaction");

DataSource ds = (DataSource) initialContext.lookup(dsName);
if (ds != null) {
System.out.println(" datasource class is " + ds.getClass().getName());
}
else {
System.out.println("ds is null, you loser");
}

System.out.println("getting connection... ");
Connection conn = ds.getConnection();
System.out.println("creating statement... ");
Statement s = conn.createStatement();
//ResultSet rs = s.executeQuery("select * from dual");
System.out.println("sleeping... ");

try {
Thread.sleep(1000 * 40);
}
catch (InterruptedException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("committing transaction");
ut.commit();
System.out.println("finished! ");
out.print("done");

%>

No comments: