I'm unable to disable caching in OpenJPA 2.0.1.
开发者_运维问答I have set the following properties in my persistence.xml:
<property name="openjpa.DataCache" value="false"/>
<property name="openjpa.QueryCache" value="false"/>
<property name="openjpa.jdbc.QuerySQLCache" value="false"/> <!-- I don't believe this is necessary -->
And I can see that the correct values of these properties are logged out when I start my app.
I have created a basic entity to test this with, with a main method that simply queries the table every second. I'm creating a new EntityManager on each iteration. When I run this against an empty TEST table, and then subsequently manually insert data into test:
insert into TEST values (1,'a');
it never picks up on the new data (although if I re-start the program it does).
import java.util.List;
import javax.persistence.*;
@Entity
@Access(AccessType.PROPERTY)
@Table(name="TEST")
public class Test {
private int id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// SIMPLE TEST CASE
public static void main(String[] args) throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("su3", null);
while(true) {
EntityManager em = factory.createEntityManager();
TypedQuery<Test> q = em.createQuery("select t from Test t", Test.class);
List<Test> res = q.getResultList();
for (Test t :res) {
System.out.println(t.getId()+", " + t.getName());
}
Thread.sleep(1000);
em.close();
}
}
}
what am I doing wrong?
EDIT1: If I create a new EntityManagerFactory inside the while loop, it works, but my understanding is that because I have set DataCache and QueryCache to false I do not need to do this, as well as it being expensive to do so.
EDIT2: I was using BoneCP as my connection pool manager, when I reverted to using DHCP or C3P0 the problem goes away. Not sure why though...
EDIT3: This is the config I was using for BoneCP:
<property name="openjpa.ConnectionDriverName" value="com.jolbox.bonecp.BoneCPDataSource"/>
<property name="openjpa.ConnectionProperties" value="DriverClassName=com.mysql.jdbc.Driver,jdbcUrl=jdbc:mysql://localhost:3306/mydb,Username=xxxx,Password=yyyy,partitionCount=3"/>
I resolved my issue by reverting back to the c3p0 driver, with the following config:
<property name="openjpa.ConnectionDriverName" value="com.mchange.v2.c3p0.ComboPooledDataSource"/>
<property name="openjpa.ConnectionProperties" value="DriverClassName=com.mysql.jdbc.Driver,jdbcUrl=jdbc:mysql://localhost:3306/dbname,characterEncoding=UTF8,useUnicode=true,user=username,password=password,autocommit=false,automaticTestTable=testtable,idleConnectionTestPeriod=60"/>
<property name="openjpa.DataCache" value="false"/>
精彩评论