Erro em transação com Spring e Hibernate!

5 respostas
andreiribas

Ae galera, não estou conseguindo fazer funcionar a transação com spring e hibernate, teoricamente dá tudo certo, salva o objeto mas não dá commit.
Estou trabalhando com anotações pra transações, não sei se é o certo, já consultei livros e a documantação mas não estou conseguindo perceber o erro.

O Código da minha interface do serviço:
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public interface CategoriaService extends DefaultService {
	@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false)
	public ReturnObject save(Categoria categoria);
	public ReturnObject getCategorias();

}
E o applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
  		
  	<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" dependency-check="none">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>  
	</bean>
    	
  	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  		<property name="sessionFactory" ref="mySessionFactory" />
	</bean>
	
	<tx:annotation-driven />

        <bean id="categoriaService" class="service.DefaultCategoriaService">
		<property name="categoriaDAO" ref="categoriaDAO"/>
	</bean>

Quem puder me ajudar...

5 Respostas

Rubem_Azenha

Eu costumo colocar as anotações na implementação em vez de na interface e funciona…

andreiribas

coloqueiu as anotações na implementação e continua não funcionando… nem o log me dá uma dica de porque não está funcionando:

Hibernate: /* insert modelo.Categoria */ insert into helpdesk.Categoria (descricao, tipo) values (?, ?) 15:23:46,828 DEBUG [AbstractBatcher ] preparing statement 15:23:46,968 DEBUG [AbstractEntityPersister] Dehydrating entity: [modelo.Categoria#<null>] 15:23:46,968 DEBUG [StringType ] binding 'asdasdasd' to parameter: 1 15:23:46,968 DEBUG [StringType ] binding 'asdasda' to parameter: 2 15:23:47,046 DEBUG [AbstractBatcher ] about to open ResultSet (open ResultSets: 0, globally: 0) 15:23:47,046 DEBUG [IdentifierGeneratorFactory] Natively generated identity: 6 15:23:47,046 DEBUG [AbstractBatcher ] about to close PreparedStatement (open PreparedStatements: 1, globally: 2) 15:23:47,046 DEBUG [AbstractBatcher ] closing statement 15:23:47,046 DEBUG [Loader ] processing result set 15:23:47,046 DEBUG [Loader ] done processing result set (0 rows) 15:23:47,046 DEBUG [AbstractBatcher ] about to close ResultSet (open ResultSets: 1, globally: 1) 15:23:47,046 DEBUG [AbstractBatcher ] about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 15:23:47,046 DEBUG [AbstractBatcher ] closing statement 15:23:47,062 DEBUG [Loader ] total objects hydrated: 0 15:23:47,062 DEBUG [StatefulPersistenceContext] initializing non-lazy collections 15:23:47,062 DEBUG [JDBCContext ] after autocommit 15:23:47,062 DEBUG [ConnectionManager ] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 15:23:47,062 DEBUG [SessionImpl ] closing session 15:23:47,062 DEBUG [ConnectionManager ] performing cleanup 15:23:47,062 DEBUG [ConnectionManager ] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 15:23:47,078 DEBUG [JDBCContext ] after transaction completion 15:23:47,078 DEBUG [ConnectionManager ] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 15:23:47,093 DEBUG [SessionImpl ] after transaction completion 15:23:47,250 DEBUG [AbstractFlushingEventListener] flushing session 15:23:47,265 DEBUG [AbstractFlushingEventListener] processing flush-time cascades 15:23:47,375 DEBUG [AbstractFlushingEventListener] dirty checking collections 15:23:47,375 DEBUG [AbstractFlushingEventListener] Flushing entities and processing referenced collections 15:23:47,468 DEBUG [AbstractFlushingEventListener] Processing unreferenced collections 15:23:47,468 DEBUG [AbstractFlushingEventListener] Scheduling collection removes/(re)creates/updates 15:23:47,468 DEBUG [AbstractFlushingEventListener] Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects 15:23:47,468 DEBUG [AbstractFlushingEventListener] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections

Não sei se tem alguma coisa a ver, mas meus daos todos tem usam o hibernateTemplate pra realizar as operações.

fiaux

Mas o problema é não estar dando commit no banco? Qual banco é? Não tem nada para configurar como auto commit true em algum lugar?

andreiribas

o banco é mysql, já conferi as tabelas estão criadas como InnoDB, as propriedades do hibernate são:

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
<hibernate-configuration>  
	<session-factory>
		<property name="connection.username">root</property>
    	<property name="connection.password">root</property>
    	<property name="connection.url">jdbc:mysql://localhost/helpdesk?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf8</property>
    	<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
		
	
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.generate_statistics">true</property>
        <property name="hibernate.use_sql_comments">true</property>
        <property name="hibernate.connection.useUnicode">true</property>
		<property name="hibernate.connection.characterEncoding">UTF-8</property>
		<property name="hibernate.c3p0.max_size">50</property>
        <property name="hibernate.c3p0.min_size">2</property>
        <property name="hibernate.c3p0.timeout">5000</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>
        <property name="hibernate.c3p0.acquire_increment">1</property>
       
        <mapping class="modelo.Franquia" />
		<mapping class="modelo.Categoria" />
		<mapping class="modelo.Usuario" />
		
	</session-factory>  
</hibernate-configuration>
andreiribas

por enquanto resolvi na pog mesmo, coloquei o autocommit = true, mas não era isso que eu queria na verdade.

Criado 11 de julho de 2008
Ultima resposta 11 de jul. de 2008
Respostas 5
Participantes 3