Retour Hibernate aléatoire
Bonjour
je suis dans le développement d'un site JavaEE avec MySql, et Hibernate (3) réagit bizarrement :
Lorsque je multiplie les appels au Dao (toujours de la même manière, en boucle, pour incrémenter un total par exemple) il arrive (1 fois sur 10) que les objets ne soient pas récupérés, ou bien dans un état précédent(avec un total faussé donc).
Le problème semblerai venir d'un cache hibernate.
J'ai ajouté des session.flush() mais ca n'a pas aidé.
Je me demande si ca peut venir de mon hibernateUtil, ou bien de mon mapping peut etre ?...
Tous vos commentaires / liens sont les bienvenus.
Merci !
HibernateUtile :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
private static Log log = LogFactory.getLog(HibernateUtile.class);
private static final SessionFactory sessionFactory;
static {
try {
// Crée la SessionFactory
sessionFactory =
new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problème de configuration : "
+ ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session getSession()
throws HibernateException {
Session s = (Session) session.get();
// Ouvre une nouvelle Session, si ce Thread n'en a aucune
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
} |
Mon dao
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
Compte compte = null;
try {
Session session = HibernateUtile.getSession();
Query query = session.createQuery("from Compte where profil=:profil and partenaire.code=:code");
query.setString("profil", profil);
query.setString("code", codePartenaire);
compte = (Compte) query.uniqueResult();
..... |
ma config hibernate
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
<!-- Mapping files -->
<mapping resource="fr/globalaction/hibernate/Action.hbm.xml" />
......
</session-factory>
</hibernate-configuration> |
un exemple de Dao
Code:
1 2 3 4 5 6 7 8 9 10
|
Session session = HibernateUtile.getSession();
Query query = session.createQuery("from Compte where profil=:profil and partenaire.code=:code");
query.setString("profil", profil);
query.setString("code", codePartenaire);
compte = (Compte) query.uniqueResult(); |