Results 1 to 6 of 6

Thread: JTA + Hibernate + Spring : Error

  1. #1

    Default JTA + Hibernate + Spring : Error

    Hi,
    I am using, Jboss 4.0.4, Hibernate 3, Spring 2.0, Java 1.5.

    I had a exception when I tried to save an object that stated tried to close hibernate session: Hibernate session already closed. So I did some searches on the forums and found out that I probably had two Transaction Manager running at the same time. One in Jboss (JTA and Hibernate Transaction Manager) and the suggestion were to take one out. I figure that I should take out the JTA and only use the Hibernate Transaction Manager that i configured in Spring. This caused me a lat of errors and exception. So I started look in to change to only use the JTA instead. I found some posting on how to configure this, but now I get another exception.

    My configuration look like this:

    <!-- DATASOURCE -->

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:OracleDS</value>
    </property>
    </bean>

    <!-- TRANSACTIONS -->

    <!-- <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>
    -->
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager">
    <property name="transactionManagerName" value="java:/TransactionManager">
    </property>
    <property name="userTransactionName" value="UserTransaction"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:/hibernate/MyCompanySessionFactory</value>
    </property>
    <property name="jtaTransactionManager">
    <bean factory-bean="transactionManager" factory-method="getTransactionManager"/>
    </property>
    </bean>

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


    Thanks
    Jacob
    Last edited by jake123; Nov 13th, 2006 at 02:03 PM.

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Since the session factory is retrieved from the JNDI and not configured through Spring, you'll no longer have LocalSessionFB but rather a sessionfactory.
    Basically, the SF is already configured and there settings a JTA property on it required creating a new one.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3

    Default

    Quote Originally Posted by Costin Leau View Post
    Since the session factory is retrieved from the JNDI and not configured through Spring, you'll no longer have LocalSessionFB but rather a sessionfactory.
    Basically, the SF is already configured and there settings a JTA property on it required creating a new one.
    Okey, so my DataSource and SessionFactory is retrieved from the JNDI. How do I configure the TransactionManager so I can work with the @Transactional annotation in my service methods?

    I guess we are stuck with the JTA from JBoss so we can not use the HibernateTransactionManager.

    This is the first time I try to configure this so I could really need some pointers, and even better some example how to configure this.

    Thanks for your help,
    Jacob

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Simply use Spring's JtaTransactionManager on top of the JNDI retrieved TM (see the JtaTM javadocs) - Spring only drives the transaction and when JTA is used, wraps the given TM (in your case JBoss') with Spring APIs.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5

    Default

    Quote Originally Posted by Costin Leau View Post
    Simply use Spring's JtaTransactionManager on top of the JNDI retrieved TM (see the JtaTM javadocs) - Spring only drives the transaction and when JTA is used, wraps the given TM (in your case JBoss') with Spring APIs.
    Here is my applicationContext.xml:

    <!-- DATASOURCE -->

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:OracleDS</value>
    </property>
    </bean>

    <!-- TRANSACTIONS -->
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager">
    <property name="jndiName">
    <value>java:/TransactionManager</value>
    </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:/hibernate/MyCompanySessionFactory</value>
    </property>
    </bean>

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

    When I deploy it I get this exception:
    14:51:09,420 ERROR [MainDeployer] Could not create deployment: file:/C:/jboss-4.0.4/server/ic/deploy/investacorp-service
    s.spring
    org.jboss.deployment.DeploymentException: Error creating bean with name 'org.springframework.transaction.interceptor.Tra
    nsactionAttributeSourceAdvisor' defined in URL [jar:file:/C:/jboss-4.0.4/server/ic/tmp/deploy/tmp423mycompany-services
    .spring!/META-INF/applicationContext-service.xml]: Cannot resolve reference to bean 'transactionInterceptor' while setti
    ng bean property 'transactionInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationExce ption:
    Error creating bean with name 'transactionInterceptor' defined in URL [jar:file:/C:/jboss-4.0.4/server/ic/tmp/deploy/tmp
    423mycompany-services.spring!/META-INF/applicationContext-service.xml]: Cannot resolve reference to bean 'transactionM
    anager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCrea
    tionException: Error creating bean with name 'transactionManager' defined in URL [jar:file:/C:/jboss-4.0.4/server/ic/tmp
    /deploy/tmp423mycompany-services.spring!/META-INF/applicationContext-persistence.xml]: Error setting property values;
    nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'jndiName' of bean class [o
    rg.springframework.transaction.jta.JtaTransactionM anager]: Bean property 'jndiName' is not writable or has an invalid se
    tter method. Does the parameter type of the setter match the return type of the getter?; - nested throwable: (org.spring
    framework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.intercepto
    r.TransactionAttributeSourceAdvisor' defined in URL [jar:file:/C:/jboss-4.0.4/server/ic/tmp/deploy/tmp423mycompany-ser
    vices.spring!/META-INF/applicationContext-service.xml]: Cannot resolve reference to bean 'transactionInterceptor' while
    setting bean property 'transactionInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationExce pt
    ion: Error creating bean with name 'transactionInterceptor' defined in URL [jar:file:/C:/jboss-4.0.4/server/ic/tmp/deplo
    y/tmp423mycompany-services.spring!/META-INF/applicationContext-service.xml]: Cannot resolve reference to bean 'transac
    tionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.Bea
    nCreationException: Error creating bean with name 'transactionManager' defined in URL [jar:file:/C:/jboss-4.0.4/server/i
    c/tmp/deploy/tmp423mycompany-services.spring!/META-INF/applicationContext-persistence.xml]: Error setting property val
    ues; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'jndiName' of bean cla
    ss [org.springframework.transaction.jta.JtaTransaction Manager]: Bean property 'jndiName' is not writable or has an inval
    id setter method. Does the parameter type of the setter match the return type of the getter?)
    at org.jboss.spring.deployment.SpringDeployer.create( SpringDeployer.java:120)
    at org.jboss.deployment.MainDeployer.create(MainDeplo yer.java:953)
    at org.jboss.deployment.MainDeployer.deploy(MainDeplo yer.java:807)
    at org.jboss.deployment.MainDeployer.deploy(MainDeplo yer.java:771)
    at sun.reflect.GeneratedMethodAccessor8.invoke(Unknow n Source)


    It would be helpful if you could give me some example code how to configure this or point me to some useful links where I can read about how to configure this...

    thanks again,
    Jacob
    Last edited by jake123; Nov 13th, 2006 at 02:27 PM.

  6. #6

    Default

    Okey, I think I got it to work now. I Can deploy everything into my JBoss server and I used one method with @Transactional annotation and it seems to work. No exceptions in the server.

    my applicationContext file look like this now:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <!-- DATASOURCE -->

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:OracleDS</value>
    </property>
    </bean>

    <!-- TRANSACTIONS -->
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager">
    <property name="transactionManagerName" value="java:/TransactionManager"></property>
    <property name="userTransactionName" value="UserTransaction"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:/hibernate/InvestacorpSessionFactory</value>
    </property>
    </bean>

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

    Is this the "correct" way of doing it and how can I test if my transaction is working?

    Thanks
    Jacob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •