• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deleted records from database by JPA

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the code:




The problem: I run this code 8 AM , and I see records on database. When I run the code 9 hours later the records from 8AM are deleted

Could you please tell me if I am right:

- Even though there is always different reference to EM , there is only one persistence context.
- By default, managed entity objects that have not been modified or removed during a transaction are held in the persistence context by weak references
- During these 9 hours garbage collector removed previous entities
- EM did not "see" entites and deleteded records from DB

Is this solution correct?

add => em.clear();  

 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several things that might cause this.

One is your JPA environmental settings. JPA can be configured to create a database when an application starts and delete it when the application terminates. Someone here had that problem not long ago.

Another possibility could be that the Database Administrator manually wiped your work for some reason.

I don't think that creating a new EntityManagerFactory every time you need an EntityManager is a good idea. As I recall, it should be created once and used any time thereafter. But I'll confess that I'm using annotated services so I don't have any explicit logic in my apps for that kind of stuff.
 
Bronislav Bronek
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using eclipseLink and I found this =

"For some reason eclipselink seems to be keeping track of the entity in its persistence context after it is persisted using persist(), even when the tx was committed and the entitymanager closed. Does anybody have a reasonable explanation for this?"


https://www.eclipse.org/forums/index.php/t/220672/

So, my solution is going to be OK when i add this ??

 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would doubt it. And it's not uncommon for a JPA environment to keep records in application-level cache for performance reasons. It's one of the things that can help it out-perform raw JDBC.

To maintain database integrity, there are certain checks made to ensure that some other app hasn't made asynchronous changes to data, and the strategy used to do those checks can be fine-tuned. But once data is committed to the database, it's not going to get deleted unless someone deletes it. Meaning either application code, external database operations, or, as I said, if you'd configured JPA to create and destroy the database when the application starts/stops.
 
Bronislav Bronek
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I explain my problam wrongly, this is what I have =

"I'm wondering why sometimes these entities are up to date in the persistence context while the transaction in which they were persisted at first was committed and the related entitymanager closed. When I do a merge, they should not be available from the persistence context according to the jpa spec, or do I need to do a clear() after persisting every entity anyway?

For some reason eclipselink seems to be keeping track of the entity in its persistence context after it is persisted using persist(), even when the tx was committed and the entitymanager closed. Does anybody have a reasonable explanation for this?"



The question , doing this after every commit to DB , is going  solve my problem =

 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a brute-force solution and should not be necessary in most conditions. If your database isn't getting updated in a timely manner, then you probably didn't correctly configure JPA. If the database is getting updates and then altered, then something else is interfering.

JPA is a standard and just because you're using a particular provider (EclipseLink) doesn't allow that provider to behave in ways that defy the standard.
reply
    Bookmark Topic Watch Topic
  • New Topic