Hibernate Database Integrity Fails with Multiple Java Applications

We have two java applications: both read / write and 3 standalone Java read / write applications (one downloads questions via e-mail, one processes the XML channel, one sends letters to subscribers), all use sleep mode and use a common database code.

The problem we recently encountered is that questions downloaded by email sometimes overwrite questions created in one of the web applications. Please note that these are separate questions that must have a separate identifier. Initially, we thought it was a cache problem. We tried to disable the second level cache, but it does not matter.

<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hibernate.cache.use_second_level_cache">false</property> 

Question:

 @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) @DocumentId public Integer getId() { return this.id; } 

We use MySQL btw.

 CREATE TABLE `question` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, ... PRIMARY KEY (`id`), ... ) ENGINE=InnoDB DEFAULT CHARSET=utf8 

We do not open or close sessions, but let hibernate manage them through Util.getSessionFactory().getCurrentSession() .

We do not want to tune the second-level cluster cache at this stage, since this creates another level of complexity, and we are more than happy with the level of performance that we receive from the application as a whole.

So does the implementation of the open-session-in-view template in web applications and manual session management in stand-alone applications do it as if to fix it?

Or any other suggestions / ideas please?

+4
source share
2 answers

It turns out this problem is not related to Hibernate at all.

One of the database tables on the staging server was filled with old data that needed to be cleared. Initially, this made it possible to rewrite the identifier, but further research proved the opposite!

As soon as we deleted the dodgy data, everything was fine.

0
source

Since all questions have identifiers, I assume that all questions are from your MySql database.

Assuming that you do not need to store the questions as transparent objects in memory, but you select all the questions for each presentation, I have one simple suggestion.

Replace the ID generator with a sequence in the database. (In the end, the ID is as a standalone in MySql). Then the database instead of applications ensures that each question will receive a unique identifier.

This solution is quite simple and reduces your complexity. And it works only if you save all incoming questions from different sources to your database, and then select them here.

If this solution gives you performance problems, you should learn more about how your Hibernate ID generator works. Hibernate provides several different generators for different scenarios.

Hope this help!

+3
source

Source: https://habr.com/ru/post/1309529/


All Articles