我有以下方法不会保留记录。如果我从servlet运行另一个查询并在其余服务更新后查看此记录,我看不到更改。有什么想法没有被坚持下去吗?我还试过flush()
和/或clear()
但没有运气:
@POST
@Path("translations")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public Translation updateTranslationValue(Translation translation,
@Context HttpServletRequest request) {
request.getSession().setAttribute("reload_xlate", true);
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
TypedQuery<Translation> query = em.createQuery(
"SELECT t FROM Translation t WHERE t.key = :key", Translation.class);
query.setParameter("key", translation.getKey());
Translation mappedTranslation = query.getSingleResult();
mappedTranslation.setValue(translation.getValue());
L.info("Updated: {}", mappedTranslation);
//em.flush();
//em.clear();
em.getTransaction().commit();
em.close();
return mappedTranslation;
}
这是我的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="MyJPAJAXRS" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>jdbc/myds</non-jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/seed.sql"/>
</properties>
</persistence-unit>
</persistence>
我的Jetty数据源是:
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/myds</Arg>
<Arg>
<New class="com.zaxxer.hikari.HikariDataSource">
<Arg>
<New class="com.zaxxer.hikari.HikariConfig">
<!--<Set name="minimumPoolSize">5</Set>-->
<!--<Set name="maximumPoolSize">20</Set>-->
<Set name="dataSourceClassName">
org.h2.jdbcx.JdbcDataSource
</Set>
<Call name="addDataSourceProperty">
<Arg>url</Arg>
<Arg>jdbc:h2:mem:test_mem</Arg>
</Call>
</New>
</Arg>
</New>
</Arg>
</New>
</Configure>
答案 0 :(得分:0)
结果显示H2,数据库池设置只需更改为1个连接:
<Set name="minimumPoolSize">1</Set>
<Set name="maximumPoolSize">1</Set>
这就是诀窍。