-->
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.  [ 2 posts ] 
Author Message
 Post subject: Monitoring the database from two programs
PostPosted: Sun Oct 19, 2008 4:01 pm 
Newbie

Joined: Sat Oct 18, 2008 6:44 pm
Posts: 2
Hibernate version: 3

Situation:

I have a program, which manage objects using Hibernate. This program has to know every changed made to the data of this objects. I also have a second program changing the data of some of the objects from the first program. This program also using Hibernate.

Problem:

My problem is to let the first program know that specified objects has been changed in the database.

As far as I understood Interceptors and events, they only works for code using the same SessionFactory. So if I have these two programs they will definitly have different SessionFactories. (Or am I wrong here?)

One solution I thought of is to refresh every X seconds. But I don't like this solution, because my program has lot of objects to monitor, but the objects won't be changed often.

Question:

Can Hibernate monitors changes in the database. Not the ones the program making it self, but the ones made direkt in the database or from another program?


Top
 Profile  
 
 Post subject: Solution
PostPosted: Sat Nov 08, 2008 7:03 pm 
Newbie

Joined: Sat Oct 18, 2008 6:44 pm
Posts: 2
Ok for everyone having the same problem. Here is what I have done. I don't like it so much, because the program has to refresh every object to look for changes, but if there aren't too much object to be monitored it should work.

Refresher

I have made a singelton (A Class with only one instance), which refreshes every given object with:
Code:
// Get the current session for refreshing
Session session = getCurrentSession();

session.beginTransaction();

// For every object to refresh
session.refresh(object);

session.getTransaction().commit();

the program has to give all object, to monitor to this class.

Object to refresh

The object, which should be refreshed need to have a setter methode in which the code is, what should happen if some data changed. In Example this could be to write it to the console:
Code:
public void setTitle(String title) {

      if (this.title != null)
         if (!this.title.equals(title))
            System.out.println("Event\t\tTitle changed");
      this.title = title;
   }

It look whether the title changed and only write it then.

Issues

One issue I see is that the program has to refresh every object to look for changes. If the program isn't time critical it should no problem because it could be donne every second or even in a less frequency.

The other problem I see is, that the objects themselfs has to organise the callback. This should be done by the refresher or the monitoring objects.

I hope it works for every having the same problem. If there are some ideas to code this I would be happy to read them here.[/b]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.