PDA

View Full Version : Question regarding Transaction flow



mathiasberg
May 9th, 2008, 02:49 AM
The thing im wondering about is, when does the transaction start and end (commit) for my web app, when doing a update?
Im using JSF1.1, Spring 2.0.8 and Hibernate 3.2.6.ga.

Example flow of a list and update page (Both on the same page).

1. User clicks save from a JSF page,
2. JSF Bean then execute a Service method. (This method is marked as @Transactional)
3. Service updates through DAO layer the hibernate object from the update page.
4. Service method return back to JSF BEAN (Is the transaction finish now, should there be a commit now?)
5. JSF Bean re-load the list of Hibernate objects to be presented on the page. (The newly updated Hibernate object should exist here, right?)
6. Return to the same JSF page. Showing the list of objects with the newly updated object.

Problem here for me is that the List of objects (5) doesnt contain the newly updated object (Done in 3). Instead the old object is loaded into the list.

Home come? if i reload the page, then the list will exist with the newly updated object value.

Could it be that the transaction doens't end until JSF page is reach and until then i cant refresh my hibernate list with the new object values?

If so, can i commit my changes just before i end the Service method (3) instead?

Im using org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter in my WEB.xml

Also, im using Springs jdbcTemplate (org.springframework.jdbc.object.StoredProcedure) to make the update and HibernateTemplate for loading... Could this mess it up for me, ?

JSF BEAN




...
public String actionSave(){

//update through jdbcTemplate
caseService.saveAndSendCaseNote(caseNote);

reload list through hibernateTemplate
loadCaseNotes();

//return to same page
return null;
}
...



Service Class


...

@Transactional
public CaseNote saveAndSendCaseNote(CaseNote caseNote){
//save
caseDaoJdbc.saveCaseNote(caseNote);
//do sendmail

return caseNote;
}
...


spring config datasouce and jdbcTemplate and hibernateTemplate and transactionManager setup



...

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>

</bean>

<!-- SessionFactory used by HibernateTemplate. Set hibernate mapping objects. -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>portal/business/CaseNote.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTempla te">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>


<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

...

mathiasberg
May 9th, 2008, 04:12 AM
This is my output in the log

From this we can see that transaction is indeed started and commited inside the service method...

See for the line, where the service method begins. "2008-05-09 10:12:19 DEBUG nu.xxx.portal.service.impl.CaseServiceImpl(line:26 6) saveAndSendCaseNote - called"


Could there be some caching function in hibernate. That i always get those in the cach instead of refreshed ones?






2008-05-09 10:12:06 DEBUG org.ajax4jsf.framework.renderer.AjaxPhaseListener( line:119) beforePhase - Process before phase INVOKE_APPLICATION(5)
2008-05-09 10:12:09 DEBUG nu.xxx.portal.web.CaseNotesBean(line:89) actionSave - called
2008-05-09 10:12:19 DEBUG org.springframework.transaction.interceptor.Abstra ctFallbackTransactionAttributeSource(line:110) getTransactionAttribute - Adding transactional method [saveAndSendCaseNote] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2008-05-09 10:12:19 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:140) getResource - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@2 a821] for key [org.hibernate.impl.SessionFactoryImpl@6b7099] bound to thread [http-8080-2]
2008-05-09 10:12:19 DEBUG org.hibernate.transaction.JDBCTransaction(line:54) begin - begin
2008-05-09 10:12:19 DEBUG org.hibernate.transaction.JDBCTransaction(line:59) begin - current autocommit status: true
2008-05-09 10:12:19 DEBUG org.hibernate.transaction.JDBCTransaction(line:62) begin - disabling autocommit
2008-05-09 10:12:19 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:168) bindResource - Bound value [org.springframework.jdbc.datasource.ConnectionHold er@257ce0] for key [org.apache.commons.dbcp.BasicDataSource@18714fe] to thread [http-8080-2]
2008-05-09 10:12:19 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:222) initSynchronization - Initializing transaction synchronization
2008-05-09 10:12:19 DEBUG org.springframework.transaction.interceptor.Transa ctionAspectSupport(line:282) prepareTransactionInfo - Getting transaction for [nu.xxx.portal.service.CaseService.saveAndSendCaseN ote]
2008-05-09 10:12:19 DEBUG nu.xxx.portal.service.impl.CaseServiceImpl(line:26 6) saveAndSendCaseNote - called
2008-05-09 10:12:47 DEBUG org.springframework.jdbc.object.SqlCall(line:156) compileInternal - Compiled stored procedure. Call string is [{call ex_cases.note_update(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}]
2008-05-09 10:12:47 DEBUG org.springframework.jdbc.object.RdbmsOperation(lin e:324) compile - RdbmsOperation with SQL [emx_cases.note_update] compiled
2008-05-09 10:12:47 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:140) getResource - Retrieved value [org.springframework.jdbc.datasource.ConnectionHold er@257ce0] for key [org.apache.commons.dbcp.BasicDataSource@18714fe] bound to thread [http-8080-2]
2008-05-09 10:12:47 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:140) getResource - Retrieved value [org.springframework.jdbc.datasource.ConnectionHold er@257ce0] for key [org.apache.commons.dbcp.BasicDataSource@18714fe] bound to thread [http-8080-2]
2008-05-09 10:12:47 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:140) getResource - Retrieved value [org.springframework.jdbc.datasource.ConnectionHold er@257ce0] for key [org.apache.commons.dbcp.BasicDataSource@18714fe] bound to thread [http-8080-2]
2008-05-09 10:12:52 DEBUG nu.xxx.portal.dao.jdbc.CaseDaoJdbc(line:194) saveCaseNote - exNoteId 1702
2008-05-09 10:13:12 DEBUG org.springframework.transaction.interceptor.Transa ctionAspectSupport(line:312) commitTransactionAfterReturning - Completing transaction for [nu.xxx.portal.service.CaseService.saveAndSendCaseN ote]
2008-05-09 10:13:12 DEBUG org.hibernate.transaction.JDBCTransaction(line:103 ) commit - commit
2008-05-09 10:13:12 DEBUG org.hibernate.transaction.JDBCTransaction(line:193 ) toggleAutoCommit - re-enabling autocommit
2008-05-09 10:13:12 DEBUG org.hibernate.transaction.JDBCTransaction(line:116 ) commit - committed JDBC Connection
2008-05-09 10:13:12 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:276) clearSynchronization - Clearing transaction synchronization
2008-05-09 10:13:12 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager(line:193) unbindResource - Removed value [org.springframework.jdbc.datasource.ConnectionHold er@257ce0] for key [org.apache.commons.dbcp.BasicDataSource@18714fe] from thread [http-8080-2]
2008-05-09 10:13:37 DEBUG nu.xxx.portal.dao.hibernate.CaseDaoHibernate(line: 445) getCaseNotes - called