Вопрос

I am working on an application consisting of Spring and Hibernate frameworks. In one particular module, the application fetches the data from database (select queries). Along with the select queries, application also issues an update statement. After further debugging, I found that the update query is fired from some TransactionInterceptor. I think, transaction interceptor is not required here as all are select queries. Can anyone please suggest me a way to disable/suppress this interceptor at runtime?

This problem might sound too abstract at first. However, I am new to this application and don't have much knowledge about it's architecture. If you need any configuration details, please let me know.

Thanks in advance.

Это было полезно?

Решение 4

I managed to suppress the update query by writing the following line in my DAO class (which was extending HibernateDAOSupport)

super.getSessionFactory().getCurrentSession().clear();

I just cleared the session as there was no update required while fetching the data and interceptor was updating the table.

Also, the original issue which I was facing is, the application was encountering org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1 from this update statement when it got executed twice (God knows why!).

Hence, we figured out that the update was never required and wanted to suppress it.

Can this fix have any consequences on the application? Is this the right way to do it? Will appreciate the inputs.

Другие советы

Can you post your application-context.xml transaction management declarations part. Where the bean : org.springframework.jdbc.datasource.DataSourceTransactionManager is defined.

If the annotaion is not enabled you should activate it like this :

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="yourDataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />

@Transactional(propagation = Propagation.NOT_SUPPORTED) on your method will disable any Spring transactions on this proxy method call. Note that by disabling the transaction you also lose other benefits, like isolation.

However, the fact that you have an update query fired is NOT because of a transaction. You are likely to encounter a different error if you simply remove the transaction (likely stale object exception when hibernate tries to update outside of a transaction, or a malfunction of some module). Hibernate does not fire spurious updates, you should look for updates to the object in question during your transaction.

Here you have the JavaDoc of the interface org.hibernate.Session method clear() :

Completely clear the session. Evict all loaded instances and cancel all pending saves, updates and deletions. Do not close open iterators or instances of ScrollableResults

So when you use clear you will clear whole the Session. That ok, you will ask me : have I only one session per transaction ? I will answer you it's depends on your application HibernateTemplate configuration, if the HibernateTemplate.alwaysUseNewSession==true but the default value is false. The solution is to not intercepte your dao method with the Transaction Manager because it will be executed by default in a non Transactional Session.

Did you get a look to the Spring Framework AOP Proxy configuration. section 10.5 Declarative transaction management

So your PlatformTransactionManager instance is HibernateTransactionManager. TransactionInterceptor will delegate the transaction handling to HibernateTransactionManager. All that means : all calls that you make to your data access methods annotated with @Transactional will be path throw spring AOP Proxy (which is a Proxy Design pattern). If you don't use annotation-based and you have declared an AOP Proxy (search for aop:config tag in your ApplicationContext.xml). So in the AOP Proxy configuration you will find the politic that your application use for intercepting data access methods and handling transactions. For finding if you are using annotation-based you should know what is 'transactionAttributeSource' : AnnotationTransactionAttributeSource or AttributesTransactionAttributeSource ?

If you have only select queries in a method you can use readOnly attribute in @Transactional annotation (@Transactional(readOnly = true)). It will be used by Spring to optimize the underlying data access layer operations (such as hibernate's "dirty" checking) and improve a perfomance. If your entity is never changes @org.hibernate.annotations.Immutable is another solution. But update statement doesn't appear without changes in entity state, so you should check your code for persistent entity changes. I am agreed that disable transaction interceptor is not a good solution.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top