Hibernate Version: Hibernate 3.1 rc3
JDK: 1.4.2
AppServer: Websphere 5.1
Database: DB2 V8
I am running a simple save to the database.
Once I get the session I set it up to FlushMode.NEVER
If I understand correctly what FlushMode.NEVER is suppose to do
"The Session is never flushed unless flush() is explicitly called by the application"
my Employee object objEmployee should not be saved since it was never explicitly flush().
But that is not what I see. my object gets saved into the database. It is like setting or
not setting the session's flush mode to NEVER does not affect the behaviour.
I looked also at the debugging statements. This is what I get witout setting the flushmode. It
makes sense to me.
Code:
14:57:06,261 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
14:57:06,261 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
14:57:06,267 INFO SettingsFactory:144 - Scrollable result sets: enabled
14:57:07,615 DEBUG AbstractBatcher:470 - closing statement
14:57:07,624 DEBUG JDBCTransaction:103 - commit
14:57:07,624 DEBUG SessionImpl:343 - automatically flushing session
14:57:07,625 DEBUG AbstractFlushingEventListener:58 - flushing session
14:57:07,628 DEBUG AbstractFlushingEventListener:111 - processing flush-time cascades
14:57:07,631 DEBUG AbstractFlushingEventListener:153 - dirty checking collections
14:57:07,631 DEBUG AbstractFlushingEventListener:170 - Flushing entities and processing referenced collections
14:57:07,642 DEBUG AbstractFlushingEventListener:209 - Processing unreferenced collections
14:57:07,643 DEBUG AbstractFlushingEventListener:223 - Scheduling collection removes/(re)creates/updates
14:57:07,643 DEBUG AbstractFlushingEventListener:85 - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
14:57:07,644 DEBUG AbstractFlushingEventListener:91 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
14:57:07,647 DEBUG Printer:83 - listing entities:
14:57:07,648 DEBUG Printer:90 - edu.miami.it.employee.Employee{value1=10431, value2=2006, id=79}
14:57:07,648 DEBUG AbstractFlushingEventListener:289 - executing flush
14:57:07,649 DEBUG AbstractFlushingEventListener:316 - post flush
14:57:07,649 DEBUG JDBCContext:195 - before transaction completion
14:57:07,649 DEBUG SessionImpl:391 - before transaction completion
14:57:07,651 DEBUG JDBCTransaction:193 - re-enabling autocommit
14:57:07,651 DEBUG JDBCTransaction:116 - committed JDBC Connection
14:57:07,652 DEBUG JDBCContext:209 - after transaction completion
14:57:07,652 DEBUG ConnectionManager:285 - aggressively releasing JDBC connection
14:57:07,653 DEBUG ConnectionManager:322 - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
14:57:07,656 DEBUG SessionImpl:421 - after transaction completion
14:57:07,657 DEBUG SessionImpl:289 - closing session
14:57:07,657 DEBUG ConnectionManager:256 - connection already null in cleanup : no action
14:57:07,658 INFO SessionFactoryImpl:727 - closing
The debugging when I set FlushMode.NEVER is different.
Code:
15:18:32,432 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
15:18:32,432 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
15:18:32,432 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
15:18:33,507 DEBUG JDBCContext:164 - no active transaction, could not register Synchronization
15:18:33,507 DEBUG SessionImpl:269 - opened session at timestamp: 4694693942976512
15:18:33,508 DEBUG SessionImpl:1138 - setting flush mode to: NEVER
15:18:33,511 DEBUG JDBCTransaction:54 - begin
15:18:33,512 DEBUG ConnectionManager:302 - opening JDBC connection
15:18:33,512 DEBUG JDBCContext:164 - no active transaction, could not register Synchronization
15:18:33,512 DEBUG JDBCTransaction:59 - current autocommit status: true
15:18:33,513 DEBUG JDBCTransaction:62 - disabling autocommit
15:18:33,514 DEBUG JDBCContext:204 - after transaction begin
15:18:33,526 DEBUG DefaultSaveOrUpdateEventListener:161 - saving transient instance
15:18:33,543 DEBUG AbstractSaveEventListener:139 - saving [edu.miami.it.employee.Employee#<null>]
15:18:33,544 DEBUG AbstractSaveEventListener:221 - executing insertions
15:18:33,589 DEBUG AbstractEntityPersister:1940 - Inserting entity: edu.miami.it.employee.Employee (native id)
15:18:33,599 DEBUG IdentifierGeneratorFactory:37 - Natively generated identity: 80
15:18:33,599 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
15:18:33,600 DEBUG AbstractBatcher:470 - closing statement
15:18:33,666 DEBUG JDBCTransaction:103 - commit
15:18:33,667 DEBUG JDBCContext:195 - before transaction completion
15:18:33,667 DEBUG SessionImpl:391 - before transaction completion
15:18:33,672 DEBUG JDBCTransaction:193 - re-enabling autocommit
15:18:33,672 DEBUG JDBCTransaction:116 - committed JDBC Connection
So even the debug statements are quite different they still both save the object.
Can somebody explain why flush mode NEVER does not work?
Here it is my hibernate configuration and the code I am using to save the object
My hibernate configuration is as follows:
Code:
<hibernate-configuration>
<session-factory >
<!-- datasource connection properties -->
<property name="connection.datasource">jdbc/DB2DevlDs</property>
<!-- dialect for DB2 -->
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereTransactionManagerLookup</property>
<mapping resource="edu/miami/it/employee/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
The code I am running is the following:
Code:
Session session = null;
Transaction tx = null;
SessionFactory sessionFactory = null;
try
{
Configuration configuration = new Configuration();
// Configure from hibernate.cfg.xml at root of classpath.
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
session.setFlushMode(FlushMode.NEVER);
tx = session.beginTransaction();
// Save object.
Employee objEmployee2 = new Employee();
objEmployee2.setValue1(new Integer(10431));
objEmployee2.setValue2(new Integer(2006));
session.save(objEmployee2);
tx.commit();
System.out.println("Employee saved!");
}
catch (HibernateException e)
{
try
{
if (tx != null)
{
tx.rollback();
}
}
catch (HibernateException ignore)
{
// ignore
}
throw e;
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException ignore)
{
// ignore
}
}
if (sessionFactory != null)
{
try
{
sessionFactory.close();
}
catch (HibernateException e)
{
// ignore
}
}
}