如何在 OpenJPA 2.0.1 中禁用缓存(编辑:BoneCP 问题)
我无法在 OpenJPA 2.0.1 中禁用缓存。
我已在我的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 -->
并且我可以看到,当我启动应用程序时,这些属性的正确值已被注销。
我创建了一个基本实体来测试它,其主要方法只是每秒查询表。我在每次迭代中创建一个新的 EntityManager。当我对空的 TEST 表运行此命令,然后手动将数据插入到测试中时:
insert into TEST values (1,'a');
它永远不会获取新数据(尽管如果我重新启动程序,它就会获取新数据)。
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();
}
}
}
我做错了什么?
EDIT1:如果我在 while 循环内部创建一个新的 EntityManagerFactory,它会起作用,但我的理解是,因为我已将 DataCache 和 QueryCache 设置为 false,所以我不需要这样做这样做,而且这样做的成本很高。
EDIT2:当我恢复使用 DHCP 或C3P0问题就消失了。但不知道为什么...
EDIT3:这是我用于 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'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"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过恢复到 c3p0 驱动程序解决了我的问题,配置如下:
I resolved my issue by reverting back to the c3p0 driver, with the following config: