随笔-11  评论-5  文章-1  trackbacks-0

我的评论

自己先跟一帖
import javax.transaction.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class JTATest
{
public static void main(String args[])
{
Context ctx=null;
Hashtable ht=new Hashtable();
DataSource ds=null;
Statement stmt=null;
ResultSet rs=null;
Connection myConn = null;
UserTransaction tx = null;
String tablename="student";
String str1="INSERT INTO student VALUES ('01001001','ixucheng', 33)";
String str2="INSERT INTO student VALUES ('01001002','hangsan', 44)";
try
{
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
ctx=new InitialContext(ht);
String str="javax.transaction.UserTransaction";
tx =(UserTransaction)ctx.lookup(str);
tx.begin();
ds = (javax.sql.DataSource)ctx.lookup("TestData");
myConn= ds.getConnection();
stmt = myConn.createStatement();
stmt.executeUpdate (str1);
stmt.executeUpdate (str2);
tx.commit();
System.out.println("Success!");
}
catch (Exception E)
{
try{tx.rollback();}catch(Exception e){}
System.out.println("事务发生错误,事务被取消! ");
}
finally
{
if (rs != null)
{
try{ rs.close(); } catch (Exception ignore) {};
}
if (stmt != null)
{
try{ stmt.close(); } catch (Exception ignore) {};
}
if (myConn != null)
{
try { myConn.close(); } catch (Exception ignore) {};
}

}
}
}