1

I'm new to using Hibernate. And my problem is: I'm developing a desktop application. This application is used in different computers. When a computer adds a new user to my database, the other computer can't see the new user until he does not close and open the application again.

I undestand that is because the sessionFactory is different.

I use Hibernate to read users.

session = Settings.sessionFactory.openSession();

String selectSql = "select c.idClient, c.name FROM Clients c WHERE c.active=1 ORDER BY c.name";

Query query = session.createQuery(selectSql);
List<Object[]> listUsers = query.list();

for (Object[] data : listUsers) 
{
     cmbClient.addItem(new ItemCombobox(data[0].toString(), data[1].toString()));
}
1
  • Is your desktop app using Hibernate internally, or do you have Hibernate in your server? Do you re-run the query periodically? Are you using the query cache? Commented Oct 21, 2015 at 11:53

3 Answers 3

1

try opening and closing a transaction every time you are done with an operation

// Non-managed environment idiom Session sess = factory.openSession(); Transaction tx = null; try {
    tx = sess.beginTransaction();

    // do some work
    ...

    tx.commit(); } catch (RuntimeException e) {
    if (tx != null) tx.rollback();
    throw e; // or display error message } finally {
    sess.close(); }

See: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html

Sign up to request clarification or add additional context in comments.

Comments

0

First of all the thougt :

I undestand that is because the sessionFactory is different.

is completly not true. I'd rather better to you read some information how java apps communicate with databases.

If you make your query you fetch data from database and have your local copy in jvm(Java virtual machine) memory. So if you want to get new copy of data from database you have to simply make a query to database once again(after on second computer the add an user operation done), not reset your application.

edit: note : the SessionFactory is only used once in whole application.

Comments

0

It is probably associated with Hibernate second-level catch. You can read about it here. From this text:

Second level cache is created in session factory scope and is available to be used in all sessions which are created using that particular session factory. It also means that once session factory is closed, all cache associated with it die and cache manager also closed down.

You can turn off second level catch, but you have to be careful as it may influence your performance. How to configure second-level catch in Hibernate, you can read here.

2 Comments

it had to do nothing with second level caching , you had to enable the second level caching yourself
@karim How do you know he didn't take the project after someone with this option already enabled?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.