Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

In java/jpa/hibernate not read the database table modified from other external program

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

primarily I apologize for my bad english.

My problem is my program don't read the record of a table modified from a external program.

My scenario is:

1. I use a table in AS400/DB2 <br/>
| TNOTA | TCOEL | TCOMM |<br/>
|  AGG  |  000  |  123  |<br/> table there is '123' in field of record;
2. start the my program, it execute a infinite while, every 30 seconds call function funTable0f();
3. at first loop, after the execution of funTable0f() the value change from '123' to 'XYZ';
4. update the record with a sql command (non with the my program) sql; update tabel0f set tcomm = 'ABC' where tnota = 'AGG and tcoel = '000';
5. at second loop, at "SECOND breackpoint", the object show me the old value 'XYZ' and not 'ABC';
6. always at second loop, at "THIRD brackpoint", the value in table remain 'ABC' and not update with 'XYZ' (hiberante remember the value is just 'XYZ' and not apply the update?)

seems it work with a copy in memory cache of table0f and not with a real table of my as400 table.






class primary key:


class model table0f:


 persistance:


entity manager factory:



DAO:


Managment:


I hope I was clear,<br/>
thanks in advance,<br/>
Riccardo














 
Saloon Keeper
Posts: 22479
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were a little too "clear", actually. Please don't bury us in code, people will just skip over it.

As I am doing for the moment. I'll just attempt to answer the simple question.

You are mostly correct. In the default JPA setup, if another process modifies a record, your app will not automatically detect the modification. In fact, SQL does not "push" modification notifications, so it wouldn't even be possible to notify your app.

On the other hand, if you changed data in that record and attempted to commit it to the database using standard transaction rules, the commit would fail with a ConcurrentModificationException, as JPA will check the database current record against what it last saw as the database current record when it was fetched.

You may need to use an XA transaction management system in such cases. Or at least catch the ConcurrentModificationException and notify the higher layers of code that the change failed and may need to be re-thought based on the external udates.
 
Tim Holloway
Saloon Keeper
Posts: 22479
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction: The JPA equivalent of "ConcurrentModificationException" is "OptimisticLockException".
 
Riccardo Marfisi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Or at least catch the ConcurrentModificationException and notify the higher layers of code that the change failed and may need to be re-thought based on the external udates.


my update function have a generic catch(Exception) but not register nothing


 
Tim Holloway
Saloon Keeper
Posts: 22479
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find this interesting: https://www.baeldung.com/jpa-pessimistic-locking
 
Riccardo Marfisi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved with EntityManager#detach

I post the solution for help some else in the future.

in main class


in managment class:




Tim Holloway wrote:You might find this interesting: https://www.baeldung.com/jpa-pessimistic-locking


Thanks for your suggestion, I will read it


Thanks,
Riccardo
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic