Results 1 to 3 of 3

Thread: Problem with Unit-Test, Rollback and 'Locking' in spring

  1. #1
    Join Date
    Jun 2005
    Posts
    4

    Default Problem with Unit-Test, Rollback and 'Locking' in spring

    Hi -

    I saw a similiar topic but unfortunatly it did not discuss the problems I am having.

    I have created a DAO and am using the
    'AbstractTransactionalDataSourceSpringContextTests ' class to create a unit test around this dao. I am 'configuring' everything in my application using spring.

    Basically this is the problem i am having and the steps

    1- I re-create the blank table I am working with.

    2- I run the test case once, expect it to rollback and leave an empty table BUT it leaves in the created entity.

    3- I run the test case again and it LOCKS (Hangs) in my DAO at the tx.commit() line (see code below). Anything that I am doing wrong in my test case or DAO would be greatly appreciated because obviously I am doing something wrong.


    Below Are the snippits of code, any thoughts would be great:

    //// the parts of the DAO in question

    public class UserDAO {

    SessionFactory sf = null;

    public UserDAO() {}

    public void setSessionFactory(SessionFactory sf) {
    this.sf = sf;
    }

    public SessionFactory getSessionFactory() {
    return this.sf;
    }

    //// where i create the user.

    public User createUser(String name, String password) throws BadParameterException {
    UserImpl u = new UserImpl();
    u.setName(name);
    u.setPasswordClearText(password);
    System.err.println("1");

    Session session = sf.openSession();// sf.openSession();
    Transaction tx = session.beginTransaction();
    try {
    System.err.println("2");
    session.save(u);
    System.err.println("3");
    tx.commit();
    System.err.println("3.5");
    } catch (Exception ex) {
    System.err.println("4");
    logger.logInternal(Level.INFO, ex, "Persisten exception");
    if (tx != null) {
    System.err.println("5");
    tx.rollback();
    return null;
    }
    } finally {
    session.close();
    }

    return u;
    }
    }

    /////////////// here is my little test program

    public class tud extends AbstractTransactionalDataSourceSpringContextTests {
    private UserDAO udo = null;

    public tud() {
    super();
    }

    protected String[] getConfigLocations() {
    String[] paths = {"serverConfig.xml"};
    return paths;
    }

    public void setUserDAO(UserDAO udo) {
    this.udo = udo;
    }

    public void testMe() {
    System.err.println("-------------------------------");
    this.deleteFromTables(new String[]{"userimpl"});
    try {
    System.err.println("+++++");
    User u = udo.createUser("nimrod", "bbbbb");
    u.setLevel(User.Level.READ);
    udo.updateUser(u);
    System.err.println("==================");
    } catch (BadParameterException e) {
    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    }

    }

    ////// and here is the spring config that i use

    <beans>
    <!-- ************************************************** ********** -->
    <!-- PostgreSQL Datasource -->
    <!-- ************************************************** ********** -->
    <!-- See http://www.mchange.com/projects/c3p0...ataSource.html -->
    <bean id="postgresDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="org.postgresql.Driver"/>
    <property name="jdbcUrl" value="jdbcostgresql://localhost/foo"/>
    <property name="user" value="foo"/>
    <property name="password" value="bar"/>
    <property name="maxPoolSize" value="25"/>
    </bean>

    <!-- ************************************************** ********** -->
    <!-- Hibernate SessionFactory -->
    <!-- ************************************************** ********** -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>com/foo/user/UserImpl.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Post greSQLDialect</prop>
    </props>
    </property>
    <property name="dataSource">
    <ref bean="postgresDataSource"/>
    </property>
    </bean>


    <bean id="userDAO" class="com.redsealsys.aurora.dao.UserDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>


    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>


    ...
    ....
    ...

  2. #2
    Join Date
    Jun 2005
    Posts
    19

    Default

    See:

    http://forum.springframework.org/showthread.php?t=14647

    I think the problem is that you are opening the Session directly, rather than letting Spring do it for you.
    Last edited by robyn; May 16th, 2006 at 04:02 AM.

  3. #3
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    lightc is correct. You should not use the transactional methods on the Hibernate Session interface, or acquire or release sessions. Spring does that for you. Please refer to the PetClinic sample application and the Spring documentation for information on how to set this up.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

Similar Threads

  1. Replies: 2
    Last Post: Jul 8th, 2005, 10:37 PM

Posting Permissions

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