J'ai une application java
où j'essaie de marquer une entité personne (correspondant à la ligne PERSON dans ma base de données) si l'enregistrement a plus d'un an. C'est à dire. définir la ligne OBSOLETE dans la base de données à "Y".Définir correctement une nouvelle connexion de base de données dans une application Java?
Je recevais l'erreur:
SQL Error: 2396, SQLState: 61000
ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-02396: exceeded maximum idle time, please connect again
WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 17008, SQLState: 99999
ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Closed Connection
C'était parce que mon DB idle timeout
est fixé à 30 minutes et l'application en cours d'exécution a été plus longtemps que cela.
Pour essayer de résoudre ce problème, je commence et ferme une nouvelle connexion pour chaque personne qui sera marquée, mais l'erreur persiste. Comment puis-je résoudre ce problème?
Méthode:
public void flagPersonIfDeletable() {
List<String> allPersonIds = getAllPersonIds();
for (String personId : allPersonIds) {
try {
startNewConnection();
flagPersonById(personId)
} finally {
closeConnection();
}
}
Démarrer une nouvelle méthode de connexion:
public void startNewConnection() {
this.Persistence.done();
this.Persistence.unbind();
this.Persistence.entityManager();
}
Fermer Méthode de connexion:
public void closeConnection() {
if (entityManager() != null &&
entityManager().isOpen()) {
if (entityManager().getTransaction().isActive()) {
entityManager().getTransaction().commit();
}
entityManager().close();
}
}
Mon persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="myPersistence">
<class>com.my.package.PersonEntity</class>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.c3p0.internal.C3P0ConnectionProvider"/>
<property name="hibernate.c3p0.max_size" value="1"/>
<property name="hibernate.c3p0.min_size" value="1"/>
<property name="hibernate.c3p0.acquire_increment" value="1"/>
<property name="hibernate.c3p0.idle_test_period" value="300"/>
<property name="hibernate.c3p0.max_statements" value="0"/>
<property name="hibernate.c3p0.timeout" value="0"/>
<property name="hibernate.c3p0.unreturnedConnectionTimeout" value="30000"/>
<property name="hibernate.c3p0.dataSourceName" value="JPA"/>
</properties>
</persistence-unit>
</persistence>