These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Query commits transaction
PostPosted: Mon Mar 29, 2010 3:03 pm 
Newbie

Joined: Mon Mar 29, 2010 2:47 pm
Posts: 4
Hi,
I have an application that executes a lot of merge actions inside a transaction.
But when I call a named query

Code:
Query query = getSession().getNamedQuery(ValorLancado.nomeQueryGetValorLancadoCreditoTransferencia);
query.setParameter(0, valorLancadoDebito.getId());
      
Object resultado = query.uniqueResult();


and my records are automatically commited, and an Exception don´t rollback my transaction.
Why this happened?


Log:

Quote:
[Controlese] 15:56:35 DEBUG DataSourceTransactionManager:371 - Creating new transaction with....
[Controlese] 15:56:35 DEBUG DataSourceTransactionManager:202 - Acquired Connection [jdbc:postgresql:....
[Controlese] 15:56:35 DEBUG DataSourceUtils:170 - Changing isolation level of JDBC Connection ....
[Controlese] 15:56:35 DEBUG DataSourceTransactionManager:219 - Switching JDBC Connection to manual commit
[Controlese] 15:56:35 DEBUG RoleHierarchyImpl:107 - getReachableGrantedAuthorities() - ....
[Controlese] 15:56:49 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
[Controlese] 15:56:49 DEBUG RoleHierarchyImpl:107 - getReachableGrantedAuthorities() - ....
Hibernate: select lancamento0_.ID_LANCAMENTO as ....
[Controlese] 15:56:49 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: select nextval ('hibernate_sequence')
[Controlese] 15:56:49 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
[Controlese] 15:56:49 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: select atribuicao0_.ID_ATRIBUICAO as ID1_1_, atribuicao0_.NOME as NOME1_ from CF_ATRIBUICAO atribuicao0_ where atribuicao0_.NOME=?
[Controlese] 15:56:49 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: select valorlanca0_.ID_VALOR_LANCADO as ID1_14....

[Controlese] 15:56:52 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: insert into CF_VALOR_LANCADO....
Hibernate: update CF_VALOR_LANCADO set ....
Hibernate: select valorlanca1_.ID_VALOR_....
It was just a select valor from...



Regards,

Elizio Rezende


Top
 Profile  
 
 Post subject: Re: Query commits transaction
PostPosted: Wed Mar 31, 2010 9:52 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Where do you see that the data is commited ? (in the log you attached there is nothing about a commit )
I believe the data is only flushed not commited.
Usually a query in hibernate makes an implicite flush in order that changed (new,deleted and updated) objects
are considered by the query (you can define the behaviour with query.setFlushMode(...))
If there are new instances to be flushed and if you defined a non-sequence identifier-generator then a separate atomic transaction may be made in order to increment the number range.
In your case you use a sequence (select nextval ('hibernate_sequence')), so there should not be a separate transaction.


Top
 Profile  
 
 Post subject: Re: Query commits transaction
PostPosted: Sat Apr 03, 2010 9:23 am 
Newbie

Joined: Mon Mar 29, 2010 2:47 pm
Posts: 4
A select in my database returned records that shouldn't have been sent.
But with setFlushMode(FlushMode.MANUAL), the hibernate stops to call insert and update...
Is it correct behavior of the transaction?

Regards,

Elizio Rezende


Top
 Profile  
 
 Post subject: Re: Query commits transaction
PostPosted: Wed Apr 07, 2010 4:58 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
A select in my database returned records that shouldn't have been sent.


If you access the database with READ UNCOMMITTED isolation level,
then you already see records which have been flushed.
This doesn't mean mandatory that regarding data is also commited.
(Please verify your isolation level )

Quote:
But with setFlushMode(FlushMode.MANUAL), the hibernate stops to call insert and update...
Is it correct behavior of the transaction?


Yes, this is the behaviour as it is described in the documentation.
In brief words:
-let your queries do a implicite flush (=default) if you want that your query considers also records which you created within the ongoing transaction.
In this case all inserts/updates will be propagated to the database before the actual query is executed.
-let your queries not do a implicite flush by calling setFlushMode(FlushMode.MANUAL),
if your query don't needs to consider eventual records which you created within the ongoing transaction.


Top
 Profile  
 
 Post subject: Re: Query commits transaction
PostPosted: Wed Apr 07, 2010 7:38 am 
Newbie

Joined: Mon Mar 29, 2010 2:47 pm
Posts: 4
My isolation level is READ UNCOMMITTED, please look at my logs.


Code:
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:371 - Creating new transaction with name [br.com.controlese.service.IValorLancadoServico.consolidar]: PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:202 - Acquired Connection [jdbc:postgresql://localhost:5432/controlese, UserName=controlese, PostgreSQL Native Driver] for JDBC transaction
[Controlese] 08:26:38 DEBUG DataSourceUtils:170 - Changing isolation level of JDBC Connection [jdbc:postgresql://localhost:5432/controlese, UserName=controlese, PostgreSQL Native Driver] to 1
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:219 - Switching JDBC Connection [jdbc:postgresql://localhost:5432/controlese, UserName=controlese, PostgreSQL Native Driver] to manual commit
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: select lancamento0_.ID_LANCAMENTO as ID1_8_0_, lancamento0_.ID_CATEGORIA as ID8_8_0_, lancamento0_.ID_CONTA as ID9_8_0_, lancamento0_.DATA_CRIACAO as DATA2_8_0_, lancamento0_.DESCRICAO as DESCRICAO8_0_, lancamento0_.ID_FAV_FONTE_PAGADORA as ID10_8_0_, lancamento0_.NUMERO_DOCUMENTO as NUMERO4_8_0_, lancamento0_.SITUACAO as SITUACAO8_0_, lancamento0_.TIPO_CONTA as TIPO6_8_0_, lancamento0_.TIPO_TRANSACAO as TIPO7_8_0_, lancamento0_.ID_USUARIO as ID11_8_0_ from CF_LANCAMENTO lancamento0_ where lancamento0_.ID_LANCAMENTO=?
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: select nextval ('hibernate_sequence')
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: select atribuicao0_.ID_ATRIBUICAO as ID1_1_, atribuicao0_.NOME as NOME1_ from CF_ATRIBUICAO atribuicao0_ where atribuicao0_.NOME=?
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
[Controlese] 08:26:38 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction
Hibernate: select valorlanca0_.ID_VALOR_LANCADO as ID1_14_0_, valorlanca0_.DATA as DATA14_0_, valorlanca0_.DATA_BAIXA as DATA3_14_0_, valorlanca0_.ID_LANCAMENTO as ID12_14_0_, valorlanca0_.SITUACAO as SITUACAO14_0_, valorlanca0_.VALOR_BAIXADO as VALOR5_14_0_, valorlanca0_.VALOR_DESCONTO as VALOR6_14_0_, valorlanca0_.VALOR_IMPOSTO as VALOR7_14_0_, valorlanca0_.VALOR_JUROS as VALOR8_14_0_, valorlanca0_.ID_VALOR_LANCADO_CREDITO as ID13_14_0_, valorlanca0_.ID_VALOR_LANCAMENTO_PAI as ID14_14_0_, valorlanca0_.VALOR_MULTA as VALOR9_14_0_, valorlanca0_.VALOR_PREVISTO as VALOR10_14_0_, valorlanca0_.VALOR_REAL as VALOR11_14_0_ from CF_VALOR_LANCADO valorlanca0_ where valorlanca0_.ID_VALOR_LANCADO=?
Hibernate: select valorlanca0_.ID_VALOR_LANCADO as ID1_14_0_, valorlanca0_.DATA as DATA14_0_, valorlanca0_.DATA_BAIXA as DATA3_14_0_, valorlanca0_.ID_LANCAMENTO as ID12_14_0_, valorlanca0_.SITUACAO as SITUACAO14_0_, valorlanca0_.VALOR_BAIXADO as VALOR5_14_0_, valorlanca0_.VALOR_DESCONTO as VALOR6_14_0_, valorlanca0_.VALOR_IMPOSTO as VALOR7_14_0_, valorlanca0_.VALOR_JUROS as VALOR8_14_0_, valorlanca0_.ID_VALOR_LANCADO_CREDITO as ID13_14_0_, valorlanca0_.ID_VALOR_LANCAMENTO_PAI as ID14_14_0_, valorlanca0_.VALOR_MULTA as VALOR9_14_0_, valorlanca0_.VALOR_PREVISTO as VALOR10_14_0_, valorlanca0_.VALOR_REAL as VALOR11_14_0_ from CF_VALOR_LANCADO valorlanca0_ where valorlanca0_.ID_VALOR_LANCADO=?
[Controlese] 08:27:06 DEBUG DataSourceTransactionManager:469 - Participating in existing transaction


At this point the method query.list() flushs the records to database
Code:
Hibernate: insert into CF_VALOR_LANCADO (DATA, DATA_BAIXA, ID_LANCAMENTO, SITUACAO, VALOR_BAIXADO, VALOR_DESCONTO, VALOR_IMPOSTO, VALOR_JUROS, ID_VALOR_LANCADO_CREDITO, ID_VALOR_LANCAMENTO_PAI, VALOR_MULTA, VALOR_PREVISTO, VALOR_REAL, ID_VALOR_LANCADO) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update CF_VALOR_LANCADO set DATA=?, DATA_BAIXA=?, SITUACAO=?, VALOR_BAIXADO=?, VALOR_DESCONTO=?, VALOR_IMPOSTO=?, VALOR_JUROS=?, ID_VALOR_LANCADO_CREDITO=?, ID_VALOR_LANCAMENTO_PAI=?, VALOR_MULTA=?, VALOR_PREVISTO=?, VALOR_REAL=? where ID_VALOR_LANCADO=?
Hibernate: select valorlanca1_.ID_VALOR_LANCADO as ID1_14_, valorlanca1_.DATA as DATA14_, valorlanca1_.DATA_BAIXA as DATA3_14_, valorlanca1_.ID_LANCAMENTO as ID12_14_, valorlanca1_.SITUACAO as SITUACAO14_, valorlanca1_.VALOR_BAIXADO as VALOR5_14_, valorlanca1_.VALOR_DESCONTO as VALOR6_14_, valorlanca1_.VALOR_IMPOSTO as VALOR7_14_, valorlanca1_.VALOR_JUROS as VALOR8_14_, valorlanca1_.ID_VALOR_LANCADO_CREDITO as ID13_14_, valorlanca1_.ID_VALOR_LANCAMENTO_PAI as ID14_14_, valorlanca1_.VALOR_MULTA as VALOR9_14_, valorlanca1_.VALOR_PREVISTO as VALOR10_14_, valorlanca1_.VALOR_REAL as VALOR11_14_ from CF_VALOR_LANCADO valorlanca0_ inner join CF_VALOR_LANCADO valorlanca1_ on valorlanca0_.ID_VALOR_LANCADO_CREDITO=valorlanca1_.ID_VALOR_LANCADO where valorlanca0_.ID_VALOR_LANCADO=?


Here I´am throwing NullPointerException just to test the transaction
Code:
[Controlese] 08:27:10 DEBUG DataSourceTransactionManager:916 - Triggering beforeCompletion synchronization
[Controlese] 08:27:10 DEBUG DataSourceTransactionManager:821 - Initiating transaction rollback
[Controlese] 08:27:10 DEBUG DataSourceTransactionManager:273 - Rolling back JDBC transaction on Connection [jdbc:postgresql://localhost:5432/controlese, UserName=controlese, PostgreSQL Native Driver]
[Controlese] 08:27:10 DEBUG DataSourceTransactionManager:945 - Triggering afterCompletion synchronization
[Controlese] 08:27:10 DEBUG DataSourceUtils:193 - Resetting isolation level of JDBC Connection [jdbc:postgresql://localhost:5432/controlese, UserName=controlese, PostgreSQL Native Driver] to 2
[Controlese] 08:27:10 DEBUG DataSourceTransactionManager:314 - Releasing JDBC Connection [jdbc:postgresql://localhost:5432/controlese, UserName=controlese, PostgreSQL Native Driver] after transaction
[Controlese] 08:27:10 DEBUG DataSourceUtils:312 - Returning JDBC Connection to DataSource
java.lang.NullPointerException
   at br.com.controlese.service.impl.ValorLancadoServico.consolidar(ValorLancadoServico.java:295)


After this rollback, a select in my database returned records that shouldn't have been sent.


Top
 Profile  
 
 Post subject: Re: Query commits transaction
PostPosted: Wed Apr 07, 2010 7:52 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
You have log debug enable on class DataSourceTransactionManager.
What is the full package name of this class DataSourceTransactionManager ?
This class seems not being part of hibernate.
Can it be that this class is buggy?
And whilst logging
Quote:
Rolling back JDBC transaction on Connection [jdbc:postgresql://localhost:5432/controlese, UserName=controlese, PostgreSQL Native Driver]

in thruth it is doing a commit ?
Do oyu have the source code of this class?


Top
 Profile  
 
 Post subject: Re: Query commits transaction
PostPosted: Wed Apr 07, 2010 8:50 am 
Newbie

Joined: Mon Mar 29, 2010 2:47 pm
Posts: 4
The full package name is org.springframework.jdbc.datasource.DataSourceTransactionManager

My applicationContext.xml
Code:
...
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
...
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        p:dataSource-ref="dataSource">
...
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
...
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
...


In my case, the transaction is committed when the method is executed completlly and no exception are thrown.
My method have this annotation
Code:
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED)


Top
 Profile  
 
 Post subject: Re: Query commits transaction
PostPosted: Wed Apr 07, 2010 9:02 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I'm not using and therefore I have no knowledge about Spring.
So unfortunately I cannot help you anymore.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.