persistence@glassfish.java.net

Re: EJB 3 trouble

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Tue, 01 Apr 2008 11:25:15 -0700

Can you try following?

em1.getTransaction().begin();
List<OurObject> sourceList = em1.createQuery(select query
here).getResultList();
em1.getTransaction().commit();

em2.getTransaction().begin();
em2.createNativeQuery(truncate existing data).executeUpdate();
for (....) {
//save data to em2
}
em2.getTransaction().commit();


Mike Fisher wrote:
> I am new to EJB 3 and I started to work with NetBeans and Glassfish
> but it seems to be difficult.
>
> Currently I am stuck on a problem and need to see if someone here can
> help me. We are trying to copy test data from one database to another
> in the init process for our tests and we keep getting exceptions about
> an XA transaction already existing. I am using MySQL and really don't
> want to change the drivers though I do need to accomplish this copy.
> The following is basically what I am doing.
>
> List<OurObject> sourceList = em1.createQuery(select query
> here).getResultList();
> em2.createNativeQuery(truncate existing data).executeUpdate();
> for (....) {
> //save data to em2
> }
>
> I keep getting an error between the query and the truncate. I have
> tried to put the following code between the two statements
>
> em1.clear();
>
> another try
>
> em1.clear();
> em1.close();
>
> another try
>
> em1.clear();
> em1.flush();
> em1.close();
>
> The only thing that I have gotten to work is if I put the query in
> another method, call it separately, and clear the em before I return.
>
> Why do I have to go through these pains and why can't I simply kill
> the first transaction easily before continuning. I have come across a
> similar problem further down in the test which I had to resolve in a
> similar fashion.
>
> Why it is so difficult to close a transaction?
>
> -=Mike
>
> ------------------------------------------------------------------------
> You rock. That's why Blockbuster's offering you one month of
> Blockbuster Total Access
> <http://us.rd.yahoo.com/evt=47523/*http://tc.deals.yahoo.com/tc/blockbuster/text5.com>,
> No Cost.