Transaction management in DBUnit

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Transaction management in DBUnit

krishnan.1000
Hi,

I have worked with DbUnit and I think it is the best tool to for db tests. I have an issue I hope DbUnit addresses this issue. In my project, we use Callable Statements to make CRUD calls. Some of the calls are complex package calls. To test crud operations, what we would like to do is

1. invoke a CRUD call (Create) and committed
2. Test if data exists
3. After the test has been executed, roll back all changes automatically unconditionally (when the test passes, fails or if an exception is thrown). From what I understand is that DatabaseOperation.INSERT should followed by DatabaseOperation.DELETE which are two separate operations but should be transactional. What I would prefer is that the changes were rolled back instead of calling delete. Because a successful insert may involve different tables with different constraints, it is very difficult for us to delete multiple entries from different tables.

Is there a facility for this purpose?

Thanks,

Kartik

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: Transaction management in DBUnit

Matthias Gommeringer
Hi there,

I think this should already be possible by just setting your jdbc connection to autocommit=false. Maybe the feature request https://sourceforge.net/tracker/index.php?func=detail&aid=564041&group_id=47439&atid=449494 also helps you a bit.

rgds,
matthias



> -----Ursprüngliche Nachricht-----
> Von: "Karthik Krishnan" <[hidden email]>
> Gesendet: 31.07.09 02:00:52
> An: [hidden email]
> Betreff: [dbunit-user] Transaction management in DBUnit

Hi,

>
> I have worked with DbUnit and I think it is the best tool to for db
> tests. I have an issue I hope DbUnit addresses this issue. In my
> project, we use Callable Statements to make CRUD calls. Some of the
> calls are complex package calls. To test crud operations, what we
> would like to do is
>
> 1. invoke a CRUD call (Create) and committed
> 2. Test if data exists
> 3. After the test has been executed, roll back all changes automaticall
> y unconditionally (when the test passes, fails or if an exception is
> thrown). From what I understand is that DatabaseOperation.INSERT
> should followed by DatabaseOperation.DELETE which are two separate
> operations but should be transactional. What I would prefer is that
> the changes were rolled back instead of calling delete. Because a
> successful insert may involve different tables with different
> constraints, it is very difficult for us to delete multiple entries
> from different tables.
>
> Is there a facility for this purpose?
>
> Thanks,
>
> Kartik
> ----------------------------------------------------------------------
> -------- Let Crystal Reports handle the reporting - Free Crystal
> Reports 2008 30-Day trial. Simplify your report design, integration
> and deployment - and focus on what you do best, core application
> coding. Discover what's new with Crystal Reports now. http://p.sf.net/
> sfu/bobj-july_______________________________________________ dbunit-
> user mailing list [hidden email] https://lists.
>
> sourceforge.net/lists/listinfo/dbunit-user


______________________________________________________
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: Transaction management in DBUnit

krishnan.1000
Matthias,

I looked through some source code. I missed this feature. Let me look into it and I will let you know how this works. Perhaps, I could leverage this feature to manage transactions outside of testing environment.

Thanks,

Karthik

On Fri, Jul 31, 2009 at 1:08 PM, Matthias Gommeringer <[hidden email]> wrote:
Hi there,

I think this should already be possible by just setting your jdbc connection to autocommit=false. Maybe the feature request https://sourceforge.net/tracker/index.php?func=detail&aid=564041&group_id=47439&atid=449494 also helps you a bit.

rgds,
matthias



> -----Ursprüngliche Nachricht-----
> Von: "Karthik Krishnan" <[hidden email]>
> Gesendet: 31.07.09 02:00:52
> An: [hidden email]
> Betreff: [dbunit-user] Transaction management in DBUnit

Hi,
>
> I have worked with DbUnit and I think it is the best tool to for db
> tests. I have an issue I hope DbUnit addresses this issue. In my
> project, we use Callable Statements to make CRUD calls. Some of the
> calls are complex package calls. To test crud operations, what we
> would like to do is
>
> 1. invoke a CRUD call (Create) and committed
> 2. Test if data exists
> 3. After the test has been executed, roll back all changes automaticall
> y unconditionally (when the test passes, fails or if an exception is
> thrown). From what I understand is that DatabaseOperation.INSERT
> should followed by DatabaseOperation.DELETE which are two separate
> operations but should be transactional. What I would prefer is that
> the changes were rolled back instead of calling delete. Because a
> successful insert may involve different tables with different
> constraints, it is very difficult for us to delete multiple entries
> from different tables.
>
> Is there a facility for this purpose?
>
> Thanks,
>
> Kartik
> ----------------------------------------------------------------------
> -------- Let Crystal Reports handle the reporting - Free Crystal
> Reports 2008 30-Day trial. Simplify your report design, integration
> and deployment - and focus on what you do best, core application
> coding. Discover what's new with Crystal Reports now. http://p.sf.net/
> sfu/bobj-july_______________________________________________ dbunit-
> user mailing list [hidden email] https://lists.
>
> sourceforge.net/lists/listinfo/dbunit-user


______________________________________________________
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: Transaction management in DBUnit

Matthias Gommeringer
In reply to this post by krishnan.1000
Sorry, I didn't completely understand what you mean. Actually all the operations like DatabaseOperation.INSERT should work within one transaction. You just have to use the same JDBC connection for insert, assert, rollback. Of course you should not commit the data after the insert as you described it initially - otherwise a rollback would not be possible.

rgds,
matthias

> -----Ursprüngliche Nachricht-----
> Von: "Karthik Krishnan" <[hidden email]>
> Gesendet: 31.07.09 23:01:01
> An: [hidden email]
> Betreff: Re: [dbunit-user] Transaction management in DBUnit

Matthias,

>
> I looked through some source code. I missed this feature. Let me look
> into it and I will let you know how this works. Perhaps, I could
> leverage this feature to manage transactions outside of testing
> environment.
>
> Thanks,
>
> Karthik
>
> On Fri, Jul 31, 2009 at 1:08 PM, Matthias Gommeringer <Matthias.
> [hidden email]> wrote:
> Hi there,
>
> I think this should already be possible by just setting your jdbc
> connection to autocommit=false. Maybe the feature request https://
> sourceforge.net/tracker/index.php?func=detail&aid=564041&group_id=
> 47439&atid=449494 also helps you a bit.
>
> rgds,
> matthias
>
> > -----Ursprüngliche Nachricht-----
> > Von: "Karthik Krishnan" <[hidden email]>
> > Gesendet: 31.07.09 02:00:52
> > An: [hidden email]
> > Betreff: [dbunit-user] Transaction management in DBUnit
>
> Hi,
> >
> > I have worked with DbUnit and I think it is the best tool to for db
> > tests. I have an issue I hope DbUnit addresses this issue. In my
> > project, we use Callable Statements to make CRUD calls. Some of the
> > calls are complex package calls. To test crud operations, what we
> > would like to do is
> >
> > 1. invoke a CRUD call (Create) and committed
> > 2. Test if data exists
> > 3. After the test has been executed, roll back all changes
> automaticall
> > y unconditionally (when the test passes, fails or if an exception is
> > thrown). From what I understand is that DatabaseOperation.INSERT
> > should followed by DatabaseOperation.DELETE which are two separate
> > operations but should be transactional. What I would prefer is that
> > the changes were rolled back instead of calling delete. Because a
> > successful insert may involve different tables with different
> > constraints, it is very difficult for us to delete multiple entries
> > from different tables.
> >
> > Is there a facility for this purpose?
> >
> > Thanks,
> >
> > Kartik
> > --------------------------------------------------------------------
> --
> > -------- Let Crystal Reports handle the reporting - Free Crystal
> > Reports 2008 30-Day trial. Simplify your report design, integration
> > and deployment - and focus on what you do best, core application
> > coding. Discover what's new with Crystal Reports now. http://p.sf.
> net/
> > sfu/bobj-july_______________________________________________ dbunit-
> > user mailing list [hidden email]://lists.
> >
> > sourceforge.net/lists/listinfo/dbunit-user
>
> ______________________________________________________
> GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://movieflat.web.de
>
> ----------------------------------------------------------------------
> --------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008
> 30-Day
> trial. Simplify your report design, integration and deployment - and
> focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now. http://p.sf.net/sfu/bobj-july
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
> ----------------------------------------------------------------------
> -------- Let Crystal Reports handle the reporting - Free Crystal
> Reports 2008 30-Day trial. Simplify your report design, integration
> and deployment - and focus on what you do best, core application
> coding. Discover what's new with Crystal Reports now. http://p.sf.net/
> sfu/bobj-july_______________________________________________ dbunit-
> user mailing list [hidden email] https://lists.
>
> sourceforge.net/lists/listinfo/dbunit-user


______________________________________________________
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: Transaction management in DBUnit

Carfield Yim
In reply to this post by krishnan.1000
I've tried to do something similar, but it turn out having very very
slow test. Instead, turn off transaction in test case and just delete
everything before next test case work better for me, btw, it was
tested in oracle, sybase and sql server, all have similar behavior.

On Fri, Jul 31, 2009 at 8:00 AM, Karthik
Krishnan<[hidden email]> wrote:

> Hi,
>
> I have worked with DbUnit and I think it is the best tool to for db tests. I
> have an issue I hope DbUnit addresses this issue. In my project, we use
> Callable Statements to make CRUD calls. Some of the calls are complex
> package calls. To test crud operations, what we would like to do is
>
> 1. invoke a CRUD call (Create) and committed
> 2. Test if data exists
> 3. After the test has been executed, roll back all changes automatically
> unconditionally (when the test passes, fails or if an exception is thrown).
> From what I understand is that DatabaseOperation.INSERT should followed by
> DatabaseOperation.DELETE which are two separate operations but should be
> transactional. What I would prefer is that the changes were rolled back
> instead of calling delete. Because a successful insert may involve different
> tables with different constraints, it is very difficult for us to delete
> multiple entries from different tables.
>
> Is there a facility for this purpose?
>
> Thanks,
>
> Kartik
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus
> on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: Transaction management in DBUnit

krishnan.1000
Hi Carfield,

Thanks for your update. I believe that Spring's testing framework has something like this but I have not investigated it. There does not seem to be a direct way to hook onto to DbUnit with Spring, but let me look into it.

Thanks,

Kartik

On Sat, Aug 1, 2009 at 8:13 AM, Carfield Yim <[hidden email]> wrote:
I've tried to do something similar, but it turn out having very very
slow test. Instead, turn off transaction in test case and just delete
everything before next test case work better for me, btw, it was
tested in oracle, sybase and sql server, all have similar behavior.

On Fri, Jul 31, 2009 at 8:00 AM, Karthik
Krishnan<[hidden email]> wrote:
> Hi,
>
> I have worked with DbUnit and I think it is the best tool to for db tests. I
> have an issue I hope DbUnit addresses this issue. In my project, we use
> Callable Statements to make CRUD calls. Some of the calls are complex
> package calls. To test crud operations, what we would like to do is
>
> 1. invoke a CRUD call (Create) and committed
> 2. Test if data exists
> 3. After the test has been executed, roll back all changes automatically
> unconditionally (when the test passes, fails or if an exception is thrown).
> From what I understand is that DatabaseOperation.INSERT should followed by
> DatabaseOperation.DELETE which are two separate operations but should be
> transactional. What I would prefer is that the changes were rolled back
> instead of calling delete. Because a successful insert may involve different
> tables with different constraints, it is very difficult for us to delete
> multiple entries from different tables.
>
> Is there a facility for this purpose?
>
> Thanks,
>
> Kartik
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus
> on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: Transaction management in DBUnit

Davis Ford-2
Spring does have a very good mechanism for this called
AbstractTransactionalDataSourceSpringContextTests

Here is an example project I set up that uses it:
http://zenoconsulting.wikidot.com/blog:8

I'm following a very similar pattern in a new project but not using
Hibernate or JPA -- just pure Spring JdbcTemplate.

Make an AbstractDbUnitTestCase something like this:

public abstract class AbstractDbUnitTestCase extends
                AbstractTransactionalDataSourceSpringContextTests {
       
        protected Object classUnderTest;
       
        /**
         * Constructor
         */
        public AbstractDbUnitTestCase() {
                ApplicationContext ctx = super.getApplicationContext();
        classUnderTest = ctx.getBean(getBeanName());
        assertNotNull(classUnderTest);
        }
       
        /**
         * (non-Javadoc)
         * @see org.springframework.test.AbstractSingleSpringContextTests#getConfigLocations()
         */
        @Override
    protected String[] getConfigLocations() {
        return new String[] { "classpath:applicationContext.xml" };
    }

        /**
         * (non-Javadoc)
         * @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction()
         */
    @Override
    protected void onSetUpInTransaction() throws Exception {
        DataSource dataSource = jdbcTemplate.getDataSource();
        Connection con = DataSourceUtils.getConnection(dataSource);
        IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
        IDataSet dataSet = new FlatXmlDataSet(new
FileInputStream("./src/test/resources/dbunit/"+getDbUnitFileName()));

        try {
            DatabaseOperation.REFRESH.execute(dbUnitCon, dataSet);
        } finally {
            DataSourceUtils.releaseConnection(con, dataSource);
        }
    }

    /**
     * You must return the filename of the dbunit test data.
     * @return the filename of the dbunit test data.
     */
    protected abstract String getDbUnitFileName();

    /**
     * You must return the bean name of the class under test.  After
the constructor
     * finishes, you will find this object in the protected field
{@code classUnderTest}
     *
     * @return the bean name of the class under test
     */
    protected abstract String getBeanName();


Then you subclass the test like this:

public MyDaoTest extends AbstractDbUnitTestCase {

   // class under test
   private MyDao dao;

   public MyDaoTest( ) {
      super();
      dao = (MyDao) super.classUnderTest;
   }

   public String getDbUnitFileName() { return "MyDaoTest.xml"; }

   public String getBeanName() { return "myDao"; }

   public void testSomething() {
      // assert dao.doesSomething()
   }

}

wire your spring bean into applicationContext.xml, and also setup a
transaction manager in spring, and you should be set to go.  Before
every test method, DbUnit will do a REFRESH, and roll it all back
after the test method.


--davis



On Mon, Aug 3, 2009 at 1:39 PM, Karthik Krishnan<[hidden email]> wrote:

> Hi Carfield,
>
> Thanks for your update. I believe that Spring's testing framework has
> something like this but I have not investigated it. There does not seem to
> be a direct way to hook onto to DbUnit with Spring, but let me look into it.
>
> Thanks,
>
> Kartik
>
> On Sat, Aug 1, 2009 at 8:13 AM, Carfield Yim <[hidden email]>
> wrote:
>>
>> I've tried to do something similar, but it turn out having very very
>> slow test. Instead, turn off transaction in test case and just delete
>> everything before next test case work better for me, btw, it was
>> tested in oracle, sybase and sql server, all have similar behavior.
>>
>> On Fri, Jul 31, 2009 at 8:00 AM, Karthik
>> Krishnan<[hidden email]> wrote:
>> > Hi,
>> >
>> > I have worked with DbUnit and I think it is the best tool to for db
>> > tests. I
>> > have an issue I hope DbUnit addresses this issue. In my project, we use
>> > Callable Statements to make CRUD calls. Some of the calls are complex
>> > package calls. To test crud operations, what we would like to do is
>> >
>> > 1. invoke a CRUD call (Create) and committed
>> > 2. Test if data exists
>> > 3. After the test has been executed, roll back all changes automatically
>> > unconditionally (when the test passes, fails or if an exception is
>> > thrown).
>> > From what I understand is that DatabaseOperation.INSERT should followed
>> > by
>> > DatabaseOperation.DELETE which are two separate operations but should be
>> > transactional. What I would prefer is that the changes were rolled back
>> > instead of calling delete. Because a successful insert may involve
>> > different
>> > tables with different constraints, it is very difficult for us to delete
>> > multiple entries from different tables.
>> >
>> > Is there a facility for this purpose?
>> >
>> > Thanks,
>> >
>> > Kartik
>> >
>> >
>> > ------------------------------------------------------------------------------
>> > Let Crystal Reports handle the reporting - Free Crystal Reports 2008
>> > 30-Day
>> > trial. Simplify your report design, integration and deployment - and
>> > focus
>> > on
>> > what you do best, core application coding. Discover what's new with
>> > Crystal Reports now.  http://p.sf.net/sfu/bobj-july
>> > _______________________________________________
>> > dbunit-user mailing list
>> > [hidden email]
>> > https://lists.sourceforge.net/lists/listinfo/dbunit-user
>> >
>> >
>>
>>
>> ------------------------------------------------------------------------------
>> Let Crystal Reports handle the reporting - Free Crystal Reports 2008
>> 30-Day
>> trial. Simplify your report design, integration and deployment - and focus
>> on
>> what you do best, core application coding. Discover what's new with
>> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
>> _______________________________________________
>> dbunit-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus
> on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>



--
Zeno Consulting, Inc.
home: http://www.zenoconsulting.biz
blog: http://zenoconsulting.wikidot.com
p: 248.894.4922
f: 313.884.2977

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user