我目前正在评估 JPA/Eclipselink 作为我们当前非常丑陋的 jdbc 数据库访问的替代方案。情况如下:
多个客户端访问同一个数据库,有时编辑相同的数据。这些数据应该定期刷新,并且只缓存很短的时间。据我了解,@Cache 注释应该正是这样做的。
以下代码基于 Vogella 上的 jpa/ eclipselink
@Entity
@Cache(expiry = 100)
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String summary;
private String description;
... getter/setter/toString omited
}
public static void main(String[] args) throws InterruptedException {
EntityManagerFactory factory =
Persistence.createEntityManagerFactory("mysql");
EntityManager em = factory.createEntityManager();
TypedQuery<Todo> q = em.createQuery("SELECT t FROM Todo t", Todo.class);
List<Todo> todoList = q.getResultList();
System.out.println("Query 1 read");
for (int i = 0; i < todoList.size(); i++) {
System.out.println("1: " + todoList.get(i));
}
Thread.sleep(30000);
TypedQuery<Todo> q2 =
em.createQuery("SELECT t FROM Todo t", Todo.class);
// q2.setHint("javax.persistence.cache.storeMode", "REFRESH");
List<Todo> todoList2 = q2.getResultList();
System.out.println("Query 2 read");
for (int i = 0; i < todoList.size(); i++) {
System.out.println("1: " + todoList.get(i));
System.out.println("2: " + todoList2.get(i));
System.out.println(todoList.get(i) == todoList2.get(i));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="mysql" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" />
<property name="javax.persistence.ddl-generation" value="create-tables" />
<property name="javax.persistence.logging.level" value="ALL" />
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
运行程序会产生以下输出:
...
[EL Finest]: query: 2013-06-19 14:30:55.897--UnitOfWork(31211079)--Thread(Thread[main,5,main])--Execute query ReadAllQuery(referenceClass=Todo sql="SELECT ID, DESCRIPTION, SUMMARY FROM TODO")
[EL Finest]: connection: 2013-06-19 14:30:55.907--ServerSession(7427424)--Connection(7633596)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Fine]: sql: 2013-06-19 14:30:55.907--ServerSession(7427424)--Connection(7633596)--Thread(Thread[main,5,main])--SELECT ID, DESCRIPTION, SUMMARY FROM TODO
[EL Finest]: connection: 2013-06-19 14:30:55.924--ServerSession(7427424)--Connection(7633596)--Thread(Thread[main,5,main])--Connection released to connection pool [default].
Query 1 read
1: Todo [id=1, summary=s1, description=d1]
1: Todo [id=2, summary=qwet, description=d2]
[EL Finest]: query: 2013-06-19 14:31:25.932--UnitOfWork(31211079)--Thread(Thread[main,5,main])--Execute query ReadAllQuery(referenceClass=Todo sql="SELECT ID, DESCRIPTION, SUMMARY FROM TODO")
[EL Finest]: connection: 2013-06-19 14:31:25.932--ServerSession(7427424)--Connection(7633596)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Fine]: sql: 2013-06-19 14:31:25.932--ServerSession(7427424)--Connection(7633596)--Thread(Thread[main,5,main])--SELECT ID, DESCRIPTION, SUMMARY FROM TODO
[EL Finest]: connection: 2013-06-19 14:31:25.934--ServerSession(7427424)--Connection(7633596)--Thread(Thread[main,5,main])--Connection released to connection pool [default].
Query 2 read
1: Todo [id=1, summary=s1, description=d1]
2: Todo [id=1, summary=s1, description=d1]
true
1: Todo [id=2, summary=qwet, description=d2]
2: Todo [id=2, summary=qwet, description=d2]
true
当线程休眠时,我更改了数据库中的一个值。我希望从数据库中获取新值,因为缓存中的值应该已经过期。似乎代码只是忽略了 @Cache 注释。我还尝试了各种其他设置,例如 type=CacheType.NONE 和 alwaysRefresh=true,但都没有改变任何东西。
当我添加 storeMode-QueryHint 时,查询总是刷新结果。这不完全是我想要的,似乎我需要将它添加到每个查询中,这很容易出错。但是忽略了Cache注解还是很奇怪的。
我也尝试使用 DescriptorCustomizer,但它也没有任何效果,尽管被使用(用断点测试)。
public void customize(ClassDescriptor descriptor) {
descriptor.alwaysRefreshCache();
descriptor.alwaysRefreshCacheOnRemote();
descriptor.disableCacheHits();
descriptor.disableCacheHitsOnRemote();
}
更新:
我正在开发的系统的一些话。它是读取/写入我们的主数据的模块。因此,例如,同一用户在内存中拥有两次用户对象是错误的。同样在我测试了@ReadOnly 注释之后,我注意到@Cache 注释的隔离参数似乎有效,但仍然没有关于过期、始终刷新等。