Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:22:05+00:00 2026-06-13T20:22:05+00:00

I have just started making a project using spring and hibernate. My DAO layer

  • 0

I have just started making a project using spring and hibernate. My DAO layer class extends HibernateDaoSupport. We are not using annotations.
Earlier, we were using struts, hence we used getTransaction, commit, rollback .. methods provided by Session class.
My requirement is very simple, for all DAO classes, if there is an exception, rollback otherwise commit. Please suggest a simplest way of introducing spring transaction management.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:22:07+00:00Added an answer on June 13, 2026 at 8:22 pm

    A few things are not clear from your question. My explanation follows based on below assumptions –

    • You are using spring to create a datasource and session factory
    • You are using Java 5 or above and could use annotations.

    Here is what your spring configuration would look like.

        <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:hsql://localhost:9001" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
    
    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="mappingResources">
            <list>
                <value>product.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.HSQLDialect
            </value>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory" />
    </bean> 
    
    <tx:annotation-driven transaction-manager="transactionManager"  />
    

    Once this is set up, you could use spring transactional annotations on your DAO methods as shown below. Spring would take care of starting transactions, committing your transactions or rolling back your transactions when exceptions are thrown. If you have business services, you would ideally use transactional annotations on your services instead of DAOs.

    @Transactional(propagation=Propagation.REQUIRED)
    public class MyTestDao extends HibernateDaoSupport {    
    public void saveEntity(Entity entity){
        getHibernateTemplate().save(entity);
    }
    @Transactional(readOnly=true)
    public Entity getEntity(Integer id){
        return getHibernateTemplate().get(Entity.class, id);
    }
     }
    

    Below code shows how transaction management could be achieve using spring’s support for AOP rather than annotations.

        <!-- Define your 'myDatasource' bean and 'mySessionFactory' bean as shown in previous code snippet -->
    <!--  Then follow the steps shown below -->
    
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />   
    
    <!-- this is the dao object that we want to make transactional -->
    <bean id="testDao" class="com.xyz.daos.MyTestDao" />
    
    <!-- the transactional advice  -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- all methods starting with 'get' are read-only -->
            <tx:method name="get*" read-only="true" />
            <!-- other methods use the default transaction settings (see below) -->
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <!-- ensure that the above transactional advice runs for any execution of 
        a method in 'daos' package-->
    <aop:config>
        <aop:pointcut id="allDaoMethods"
            expression="execution(* com.xyz.daos.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethods" />
    </aop:config>
    

    For additional details, please see –
    Spring Declarative Transactions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started using JSON... I am using it in my project (Spring).
we have just started using a git account of our Django website project so
I have just started using RSpec and I copied the very simple test on
I have just started implementing ISet 's instead of IList 's in my project
I have just started using flexigrid and wanted to use the resource string to
I have just started learning c# in xna and I'm making a simple tower
I have just started making a wp-site . I have a small problem with
I have recently started making a C++ Wrapper of GTK+ (Nothing special, just wrapping
I've been making my first ASP.NET Visual Studio website and I have just started
I have just started using Rhino mocks and I am having difficulty doing that.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.