Cache Problem - Writing database data to Java system

Asked

Viewed 303 times

2

I have a system developed in Java using JPA, Hibernate, Primefaces. When I write system registration reflects in the Mysql database normally, until here OK, but when I do unlike the database (registration or change) in the application with several F5 it does not update, always needing to restart Tomcat. I know it’s not much information, but does anyone know where I should start? Summarizing from Java system to Mysql database everything ok, from database to system does not update with F5.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

<persistence-unit name="rtin">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>aqui esta o meu dominio</class>

    <shared-cache-mode>NONE</shared-cache-mode>

    <properties>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />

        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/teste" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="root" />
    </properties>
</persistence-unit>

Request context:

public class RequestContext {

protected static final Logger logger = Logger.getLogger(RequestContext.class.getName());

protected Locale userLocale;
protected boolean replaceableSecurityContext = true;
protected EntityTransaction entityTransaction;
protected EntityManager entityManager;

/**
 * Instanciação de ThreadLocal para manter o estado associado à thread
 * corrente
 */
protected static ThreadLocal<RequestContext> instance = new ThreadLocal<RequestContext>() {
    protected RequestContext initialValue() {
        return null;
    }
};

public RequestContext() {
    this.entityTransaction = null;
    this.entityManager = null;
}

public static RequestContext getCurrentInstance() {
    if (instance.get() == null) {
        throw new IllegalStateException(
                "UserContext deve ser inicializado antes desde método ser invocado. Verifique a inicialização do UserContext.");
    }

    return instance.get();
}

public static void invalidateCurrentInstance() {
    instance.set(null);
    System.out.println("===== invalidated context");
}

public static void setupUserContext() {
    RequestContext ctx = new RequestContext();
    instance.set(ctx);
}

public void commitGlobalEntityTransaction() {
    if (entityTransaction != null) {
        if (entityTransaction.isActive()) {
            entityTransaction.commit();
            System.out.println("===== commited transaction");
        }
        entityTransaction = null;
    }
}

public void rollbackGlobalEntityTransaction() {
    if (entityTransaction != null) {
        if (entityTransaction.isActive()) {
            entityTransaction.rollback();
            System.out.println("===== rolled back transaction");
        }
        entityTransaction = null;
    }
}

public EntityTransaction resolveGlobalEntityTransaction() {
    if (entityTransaction == null) {
        EntityManager em = resolveDefaultEntityManager();
        entityTransaction = em.getTransaction();
    }
    return entityTransaction;
}

public void beginGlobalEntityTransaction() {
    if (!resolveGlobalEntityTransaction().isActive()) {
        resolveGlobalEntityTransaction().begin();
        System.out.println("===== began new transaction");
    }
}


public EntityManager resolveDefaultEntityManager() {
    if (entityManager == null) {
        entityManager = PersistenceAgent.createDefaultEntityManager();
        System.out.println("===== created entity manager");
    }
    return entityManager;
}

public void closeDefaultEntityManager() {
    if (entityManager != null) {
        entityManager.close();
        entityManager = null;
    }
     System.out.println("===== closed entity manager");
}

Domainentity class

@MappedSuperclass 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class DomainEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Transient
protected String cannotEditReason;

@Transient
protected String cannotDeleteReason;

public abstract Long getId();

getters e setters equals e hashCode

Classe Rotina

@Entity 
@Table(name = "rotina")
public class Rotina extends DomainEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue
private Long id;

private String nome;

getters e setters

Classe Pessoa

@Entity 
@Table(name = "pessoa")
public class Pessoa extends DomainEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue
private Long id;

private String chave;
private String nome;

@OneToMany(mappedBy = "pessoaResponsavel")
private Collection<Rotina> rotinas = new ArrayList<Rotina>();

getters e setters

Execution Classroutine

@Entity 
@Table(name = "execucao_rotina")
public class ExecucaoRotina extends DomainEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue
private Long id;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "data_confirmacao_execucao")
private Date dataConfirmacaoExecucao;

@ManyToOne
private Rotina rotina;

getters e setters

2 answers

1

1


After thinking that I would need to make notes in the model classes, I saw that the solution to my case was quite simple, see:

In my persistence.xml was just add:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.connection.autocommit" value="true"/>

Complete persistence was:

<persistence-unit name="rtin" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>meu dominio</class>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.connection.autocommit" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />

        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/rotinas_diman" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="root" />

    </properties>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.