tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Piotr D <>
Subject EJB repository testing with OpenEJB - how to rollback changes
Date Tue, 05 Apr 2011 20:16:14 GMT

I try to test my EJB-based repositories using OpenEJB. Every time new
unit test is runned I'd like to have my DB in an "initial" state.
After the test, all changes should be rolled back (no matter if test
succeeded or not). How to accomplish it in a simple way? I tried using
UserTransaction - beginning it when test is starting and rolling back
changes when finishing (as you can see below). I don't know why, but
with this code all changes in DB (which were done during unit test)
are left after line rolling changes back has been executed. As I
wrote, I'd like to accomplish it in the simplest way, without any
external DB schema and so on.


 public class MyRepositoryTest {

    private Context initialContext;

    private UserTransaction tx;

    private MyRepository repository; //class under the test

    public void setUp() throws Exception {
        this.initialContext = OpenEjbContextFactory.getInitialContext();
        this.repository = (MyRepository) initialContext.lookup(
        TransactionManager tm = (TransactionManager) initialContext.lookup(
        tx = new CoreUserTransaction(tm);

    public void tearDown() throws Exception {
        this.initialContext = null;

    public void test() throws Exception {
        // do some test stuff


I've tried also in this way:
    public void setUp() throws Exception {
        tx = (UserTransaction)

But result is the same (changes are persisted into data storage and no
rolled back). I use the latest (3.1.4) OpenEJB version.

Thanks in advance for any hints!


View raw message