-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: JPA + JTA requiring flush after persisting object with FK
PostPosted: Tue Sep 06, 2016 1:32 am 
Newbie

Joined: Sat May 31, 2008 11:39 pm
Posts: 4
I've recently added JTA (Atomikos, but had same problem with Bitronix) to my spring+Hibernate JPA application in order to have XA support, and I've run into a very odd problem which after MANY hours of debugging I've managed to track down the source.

Basically everything works fine until I try to persist an object with a foreign key relationship (with or without cascade, makes no differenc). For example:

Code:
    @Entity
    public class TestObj {
        @Id
        private int id;
        @OneToOne
        private TestObj2 testObj2;
   
        public TestObj() {
        }
   
        public TestObj(int id) {
            this.id = id;
        }
   
        public TestObj(int id, TestObj2 testObj2) {
            this.id = id;
            this.testObj2 = testObj2;
        }
   
        public int getId() {
            return id;
        }
   
        public void setId(int id) {
            this.id = id;
        }
    }

    @Entity
    public class TestObj2 {
        @Id
        private int id;
   
        public TestObj2() {
        }
   
        public TestObj2(int id) {
            this.id = id;
        }
   
        public int getId() {
            return id;
        }
   
        public void setId(int id) {
            this.id = id;
        }
    }

    public void testDb() {
        tx.execute(status -> {
            entityManager.persist(new TestObj3(0));
            TestObj2 o2 = new TestObj2(0);
            entityManager.persist(o2);
            TestObj o1 = new TestObj(0, o2);
            entityManager.persist(o1);
            // If i flush the problem disapears
            // entityManager.flush();
            return null;
        });
    }


When i try to do so, it gives me an error:

ERROR: HHH000346: Error during managed flush [Session/EntityManager is closed]

No other logs or indication of what the cause of the entity manager being closed before the end of the transaction.

After trying many things, I found out that if i flush the entity manager, that fixes the problem, although i did specifically persist both objects. This differs from when i just use normal JPA transactions (flush not required)

Anyone knows why this is happening, and is this really the only way around the problem when using JTA ? Also does anyone knows if doing the entityManager.flush impact performance (I would have thought it doesn't?)

I've got a project with unit tests that replicate the problem: https://github.com/Kloudtek/ktspring (the unit tests are those for standalone/atomikos-artemis-hibernate

UPDATE:

To make matters worse, i've got once case where flushing isn't help... trying to persist the object with foreign key just causes the entity manager to get closed no matter how many flushes are before/after


Top
 Profile  
 
 Post subject: Re: JPA + JTA requiring flush after persisting object with FK
PostPosted: Thu Sep 08, 2016 9:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
This is a very strange behavior that you experience. Try adding a break point and check who is triggering the EntityManager.close() method.

You need to post the Hibernate properties you are using and also the tx.execute code.


Top
 Profile  
 
 Post subject: Re: JPA + JTA requiring flush after persisting object with FK
PostPosted: Thu Sep 08, 2016 1:09 pm 
Newbie

Joined: Sat May 31, 2008 11:39 pm
Posts: 4
It's a spring app so it's been closed by transaction synchronizer.

To be quite honest I suspect this is a spring issue (I've submitted the same question to spring questions) but I hope hoping this issue has been seen by hibernate users / devs


Top
 Profile  
 
 Post subject: Re: JPA + JTA requiring flush after persisting object with FK
PostPosted: Fri Sep 09, 2016 1:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Isn't it closed because there's some exception being thrown?


Top
 Profile  
 
 Post subject: Re: JPA + JTA requiring flush after persisting object with FK
PostPosted: Wed Sep 14, 2016 2:12 pm 
Newbie

Joined: Sat May 31, 2008 11:39 pm
Posts: 4
Nope not anywhere i can see, in fact it looks like this is happening even before it touches JDBC (I put a breakpoint in statement class).......


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.