Very basic question for a newbie in JTA

Hello,

I would like to implement DB transaction using JTA implementation of my app server (Weblogic 6.1). If there are n statements, I want them all to be executed and succeed. If one fails, all the changes made to the DB need to be rolled back. Very simple. The most basic transaction you can think of.

My code which you can see below, gets a context from the app server, create a UserTransaction object, gets a connection and a statement and finally executes the statements.

- Autocommit has to be set to false for the connection (since we want all or none of the statements to succeed).

The problem

I get a return code from the executeUpdate method that 1 row was inserted. But, when I check the DB, nothing is inserted in it. Can someone please tell me why? Thanks.

Code

--

import javax.transaction.UserTransaction;

import javax.transaction.NotSupportedException;

import javax.transaction.SystemException;

import java.sql.*;

import javax.sql.*;

import javax.naming.*;

import java.util.*;

import weblogic.jndi.*;

public class JTATest {

public static void main(String args[]){

Context ctx = null;

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

env.put(Context.PROVIDER_URL, "t3://localhost:7001");

env.put(Context.SECURITY_PRINCIPAL, "system");

env.put(Context.SECURITY_CREDENTIALS, "");

try {System.out.println("Loading Initial Context...");

ctx = new InitialContext(env);

System.out.println("Done loading Initial Context");

UserTransaction txn = (UserTransaction) ctx.lookup("javax.transaction.UserTransaction");

try {

txn.begin();

javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("DCMDataSource");

Connection con = ds.getConnection();

con.setAutoCommit(false);

Statement stmt = con.createStatement();

System.out.println("Got connection and statement");

int i = stmt.executeUpdate("insert into yahoo values(1,'foo')");

stmt.close();

txn.commit();

System.out.println("Committed Transaction");

}

catch(SQLException se){

se.printStackTrace(System.err);

try {

txn.rollback();

} catch (javax.transaction.SystemException se){

se.printStackTrace(System.err); }

}

catch(javax.transaction.NotSupportedException txnNotSupported){

txnNotSupported.printStackTrace(System.err);

}

catch(javax.transaction.RollbackException re){

re.printStackTrace(System.err);

}

catch(javax.transaction.SystemException sysExcep){

sysExcep.printStackTrace(System.err);

}

catch(javax.transaction.HeuristicMixedException he){

he.printStackTrace(System.err);

}

catch(javax.transaction.HeuristicRollbackException her){

her.printStackTrace(System.err);

}

}

catch(NamingException ne){

ne.printStackTrace(System.err);

}

}

}

[3082 byte] By [chessplayera] at [2007-9-19]
# 1

Hi,

Are you sure that you are using an XADataSource in the configuration? If not, then the JTA commit will not be propagated to the corresponding database transaction. And because autocommit is false, the database will not see _any_ commit at all for the update you made. So: afterwards, the transaction will be rolled back, and the update lost.

Hope that helps,

Guy

GuyPardona at 2007-7-8 > top of java,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Thanks Guy.I am using Weblogic 6.1. I have configured a regular Datasource in the console. How do I tell if it is a XADataSource? So, I am correct to assume that JTA can be used without using EJBs etc. Thanks.
chessplayera at 2007-7-8 > top of java,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> Thanks Guy.

>

> I am using Weblogic 6.1. I have configured a regular

> Datasource in the console. How do I tell if it is a

> XADataSource?

>

> So, I am correct to assume that JTA can be used

> without using EJBs etc. Thanks.

>

If the driver you are using is not XA compatible, than you can still set up a TxDatasource in the WLS console.

I faced a situation where database inserts, updates and such were leaving the database in an inconsistent state because our container-managed transactions did not seem to be working. Deleting our existing "regular" Datasource and replacing it with a datasource under the "TxDatasource" folder in the WLS console did the trick. I think you also have to enable "two-phase commit" when you set up the TxDatasource. It is a checkbox in the configuration for the datasource.

I would assume that this is also the case for bean-managed transactions.

ccuozzoa at 2007-7-8 > top of java,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

Hi,

I wonder Can I use JTA without using EJBs. I mean to say my application has only servlets,jsps,classes and java beans, no EJBS. So can I setup a TXDatasourcename and setup required properties under JTA tab of WLS6.1 and use in my apllication. If so any suggestions......?

Thanks in advance..

sidlicha at 2007-7-8 > top of java,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Sorry this may not relate to the problem directly but I am curious to know why you havent closed the connection.
jalalima at 2007-7-8 > top of java,Database Connectivity,Java Database Connectivity (JDBC)...