I am having an issue with guice-persist and guice-servlet (http-request scoped jpa sessions) where I attempt to update an entity's value and persist that update, but the update is never persisted to the database. I have tried forcing the write with an entityManager.flush()
and entityManager.getTransaction().commit()
, but when I look in the logs nothing seems to happen, even when the http session ends and the jdbc connection is released.
I would normally expect to see hibernate issuing a sql update statement, but the update never seems to register. What strikes me as odd is that I have no problem creating new entities, this only seems to be effecting updates.
I have a Singleton scoped servlet that has an injected UserDao, which uses an injected Provider<EntityManager>
.
Here is my persistence.xml:
<persistence 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"
version="2.0">
<persistence-unit name="db-manager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.turms.server.database.DurableUser</class>
<properties>
<!-- Disable the second-level cache -->
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!--<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>-->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/TestService"/>
<property name="hibernate.connection.username" value="xxxxx"/>
<property name="hibernate.connection.password" value="xxxxx"/>
<property name="hibernate.connection.pool_size" value="1"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<!-- Default is false for backwards compatibility. Should be used on all new projects -->
<property name="hibernate.id.new_generator_mappings" value="true"/>
</properties>
</persistence-unit>
I have replicated this problem using MySQL and Derby databases.
Here is an example of an update attempt that fails:
public boolean testUpdate(DurableUser user) {
entityManager.get().getTransaction().begin();
String testUpdateString = "askdjfaskdjfalsdkf";
user.setField(testUpdateString);
entityManager.get().persist(user);
log.info("user field persisted");
entityManager.get().flush();
entityManager.get().getTransaction().commit();
entityManager.get().clear();
return true;
}
It takes a DurableUser
(created w/o issue in a previous http session) and updates a field. Even with an explicit flush()
and commit()
, hibernate issues no update statement.
I noticed in the logs that org.hibernate.internal.util.EntityPrinter
does log the user's toString()
, which shows the updated field. Would that mean hibernate does recognize the entity has been dirtied and is still not persisting the changes?
Can anybody answer why I can successfully create new entities but not update existing entities? I'm completely stumped so far.
EDIT: Here are the logs from the session:
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7 spi.AbstractTransactionImpl - begin
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7 jdbc.JdbcTransaction - initial autocommit status: true
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7 jdbc.JdbcTransaction - disabling autocommit
2013-12-04 20:56:10,881 INFO http-apr-8080-exec-7 dao.UserDao - user field persisted
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Processing flush-time cascades
2013-12-04 20:56:10,881 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Dirty checking collections
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7 internal.Collections - Collection found: [com.xxx.server.database.DurableUser.cards#7], was: [com.xxx.server.database.DurableUser.cards#7] (initialized)
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7 internal.Collections - Collection found: [com.xxx.server.database.DurableUser.donations#7], was: [com.xxx.server.database.DurableUser.donations#7] (initialized)
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7 internal.Collections - Collection found: [com.xxx.server.database.DurableUser.installments#7], was: [com.xxx.server.database.DurableUser.installments#7] (initialized)
2013-12-04 20:56:10,882 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 3 collections
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 util.EntityPrinter - Listing entities:
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 util.EntityPrinter - com.xxx.server.database.DurableUser{donations=[], installments=[], id=7, username=test, name=test testerson, passwordChangeKey=askdjfaskdjfalsdkf, cards=[]}
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 spi.AbstractTransactionImpl - committing
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Processing flush-time cascades
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Dirty checking collections
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 internal.Collections - Collection found: [com.xxx.server.database.DurableUser.cards#7], was: [com.xxx.server.database.DurableUser.cards#7] (initialized)
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 internal.Collections - Collection found: [com.xxx.server.database.DurableUser.donations#7], was: [com.xxx.server.database.DurableUser.donations#7] (initialized)
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 internal.Collections - Collection found: [com.xxx.server.database.DurableUser.installments#7], was: [com.xxx.server.database.DurableUser.installments#7] (initialized)
2013-12-04 20:56:10,883 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2013-12-04 20:56:10,884 DEBUG http-apr-8080-exec-7 .AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 3 collections
2013-12-04 20:56:10,884 DEBUG http-apr-8080-exec-7 util.EntityPrinter - Listing entities:
2013-12-04 20:56:10,886 DEBUG http-apr-8080-exec-7 util.EntityPrinter - com.xxx.server.database.DurableUser{donations=[], installments=[], id=7, username=test, name=test testerson, passwordChangeKey=askdjfaskdjfalsdkf, cards=[]}
2013-12-04 20:56:10,886 DEBUG http-apr-8080-exec-7 jdbc.JdbcTransaction - committed JDBC Connection
2013-12-04 20:56:10,886 DEBUG http-apr-8080-exec-7 jdbc.JdbcTransaction - re-enabling autocommit
2013-12-04 20:56:11,613 DEBUG http-apr-8080-exec-7 internal.LogicalConnectionImpl - Releasing JDBC connection
2013-12-04 20:56:11,614 DEBUG http-apr-8080-exec-7 internal.LogicalConnectionImpl - Released JDBC connection