-
1. Re: Problem with UserTransaction.
vickyk Dec 4, 2002 1:17 AM (in response to maramonar)Hi,
Please refer to this code as this has been working
DB TEST FOR JBOSS
<%@ page import="javax.naming.*,javax.sql.*,java.sql.*,project.session.login.*,javax.transaction.*" %>
<%
Context ocontext=new InitialContext();
UserTransaction trans=(UserTransaction)ocontext.lookup("UserTransaction");
//trans.setTransactionTimeout(20);
trans.begin();
DataSource ods=(DataSource)ocontext.lookup("java:/OracleDSI");
Connection ocon=ods.getConnection();
System.out.println("First");
PreparedStatement ostmt=ocon.prepareStatement("update gangsterejb set name=? where id = ? ");
ostmt.setString(1,request.getParameter("name"));
ostmt.setInt(2,3);
ostmt.executeUpdate();
System.out.println("Second");
ocon.close();
/*
DataSource ods=(DataSource)ocontext.lookup("java:/XAOracleDS");
System.out.println("After getting DataSource");
Connection ocon=ods.getConnection();
System.out.println("After getting Connection"+ocon.getAutoCommit());
PreparedStatement ostmt=ocon.prepareStatement("update gangsterejb set name=? where id = ? ");
ostmt.setString(1,"VK1");
ostmt.setInt(2,3);
//ostmt.executeUpdate();
System.out.println("After update");
System.out.println("Before Starting Commit");
*/
if(request.getParameter("rollback")==null)
trans.commit();
else
trans.rollback();
System.out.println("After Committing");
out.println(trans+"");
%>
<%
ods=(DataSource)ocontext.lookup("java:/DB2DS");
Connection ocon1=ods.getConnection();
//out.println("DB2 "+ocon);
%>
<%--
ResultSet ors=ostmt.executeQuery("select * from tab");
while(ors.next())
{
%>
<%= ors.getString(1) %>
<%
}
--%>
<%-- Try this Sundar--%>
<%--
LoginHome ohome=(LoginHome)ocontext.lookup("Login");
Login oremote=ohome.create();
out.println(oremote.login("Vicky","Comsoft"));
//RolesHome orhome=(RolesHome)ocontext.lookup("Roles");
//Roles orremote=orhome.create(new Integer(5));
//out.println(oremote);
--%>
You are getting the connectionn from persistence manager,you dont have to do.Refer to the specification of JTS you will notice there are ResourceManager,TransactionManager and Applicaiton Component.So you should try to get the connection from the TransactionManager which I am doing not from the PersistenceManager.
Regards
Vicky -
2. Re: Problem with UserTransaction.
maramonar Dec 4, 2002 7:02 AM (in response to maramonar)Hi Vicky, thanks for your reply.
PersistenceManager is a plain java object from a persistence framework developed for us.
The problem was in the order of the sentences.
Reading your code, the get the Connection are after the begin() of UserTransaction. I have changed this in my code, and now all work fine.
Thanks a lot.