我有一个带有cxf 3.0.1框架和openjpa 2.3.0的休息网络应用程序。 web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Mobile app rest services</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
persistence.xml:
<?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="thePU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>xz.Partner</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="openjpa.DataCache" value="true(
Types=
/*selected classes*/
EvictionSchedule='0 0 * * *')"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://some.remote.rds.amazonaws.com:3306/XZdb?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="XXX"/>
<property name="javax.persistence.jdbc.password" value="XXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.validation.mode" value="NONE"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.ConnectionFactoryProperties"
value="PrettyPrint=true, PrintParameters=true"/>
<property name="openjpa.Compatibility" value="default(flushBeforeDetach=false)"/>
</properties>
</persistence-unit>
</persistence>
然后我定时用System.currentTimeMillis()
执行一些查询
在执行过程之前和之后,它给出了与openjpa trace log
2333 thePU TRACE [qtp1769466090-14] openjpa.jdbc.SQL - <t 2143135982, conn 1825478533> [164 ms] spent
2015-06-15[INFO]xz.orm.dao.impl.util.PerfMeasureUtil stop: :: exec query time: 824ms
[164 ms]花费 vs 执行查询时间:824毫秒
这是一个连接大约4个表的本机查询。 我自己管理entityManager生命周期 - 在请求结束时在开始和结束时创建。
/**
* Enables the derived classes to execute read only operation using the entityManager
*
* @param <T>
* @param operation
* @return
*/
protected <T> T executeNonTransactionOperation(SimpleOperation<T> operation) {
try {
EntityManager entityManager = PersistenceHelper.getEntityManager();
T result = operation.execute(entityManager);
return result;
} catch (Exception e) {
//TODO: throw our custom exception?
throw e;
} finally {
PersistenceHelper.closeEntityManager();
}
}
/**
* Enables the derived classes to execute operation (inside transaction) using the entityManager
*
* @param <T>
* @param operation
* @return
*/
protected <T> T executeTransactionOperation(SimpleOperation<T> operation) {
try {
EntityManager entityManager = PersistenceHelper.getEntityManager();
PersistenceHelper.beginTransaction();
T result = operation.execute(entityManager);
PersistenceHelper.commit();
return result;
} catch (Exception e) {
PersistenceHelper.rollback();
//TODO: throw our custom exception?
throw e;
} finally {
PersistenceHelper.closeEntityManager();
}
}
如果openjpa声明它执行得那么快,为什么会有这样的差异呢?
答案 0 :(得分:2)
问题在于您正在测量两种不同的东西。164 ms
是数据库执行查询所花费的时间。我怀疑你测量的824 ms
是你的实体对象的查询执行+实例化。