TransactionScope Class - Is there an equivalent in JAVA?

J

JT

In .NET I have been using the TransactionScope class to wrap my unit tests
into a transaction and roll back any database changes after I have completed
my unit test. Now I am dealing with some JAVA developers and looking for a
similar way for them to be able to do this from their JAVA code and not at
the database level. Is there and equivalent to this in JAVA? Thanks.
 
J

Jon Skeet [C# MVP]

JT said:
In .NET I have been using the TransactionScope class to wrap my unit tests
into a transaction and roll back any database changes after I have completed
my unit test. Now I am dealing with some JAVA developers and looking for a
similar way for them to be able to do this from their JAVA code and not at
the database level. Is there and equivalent to this in JAVA? Thanks.

There are various transaction types available in Java (only the stock
ticker is capitalized btw). It depends on which framework you're using.
For instance, Spring provides its own transaction scoping objects, as
does J2EE IIRC.
 
J

Jon Skeet [C# MVP]

JT said:
They want to use EJB3 which I do not know anything about. Thanks.

Okay - there's certainly the concept of transaction scopes within EJB3,
although I can't remember the details. If you show them your .NET code
and explain what it does, they should be able to do a similar thing in
Java.

Personally I don't do that though - I create appropriate conditions in
the unit test database at the start of each test, but don't remove
anything at the end. Each test needs to be able to set itself up
completely. This is less efficient, but does have the advantage that if
a test fails, you can see the state of the database at the point of
failure. It also means you can test code which *does* commit
transactions :)
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

JT said:
In .NET I have been using the TransactionScope class to wrap my unit tests
into a transaction and roll back any database changes after I have completed
my unit test. Now I am dealing with some JAVA developers and looking for a
similar way for them to be able to do this from their JAVA code and not at
the database level. Is there and equivalent to this in JAVA? Thanks.

The closest in the Java world must be the UserTransaction class.

Note that it is a JEE class not a JSE class.

Besides the fact that it is really an interface and that you must lookup
an implementation in JNDI instead of a class with a constructor,
then is has the same basic functionality that connections used
get enlisted in the transaction that can then by either committed
or rolled back.

It is very rare to use UserTransaction. It is almost only used with
session beans with BMT.

Session beans with CMT, entity beans 1.x & 2.x and
JDO/Hibernate/JPA (entity beans 3.x) all uses transactions
differently.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jon said:
Okay - there's certainly the concept of transaction scopes within EJB3,
although I can't remember the details.

EJB certainly has transactions support, but EJB transactions
stuff (except for BMT) are much more like the stuff in
System.EnterpriseServices.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top