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.  [ 9 posts ] 
Author Message
 Post subject: disappearing data
PostPosted: Wed Aug 18, 2004 2:14 pm 
Newbie

Joined: Wed Aug 18, 2004 1:33 pm
Posts: 5
I am using Hibernate 2.1.5 with MySQL 4.0.14-max, running in Resin 3.0.7. Any pointers for the following situation would be appreciated.

Rows are added to a database table using Hibernate. I can query the table and see the data I just entered through my form. However, when I reload my webapp, the data disappears.

Configuration
Code:
    <session-factory name="java:comp/env/hibernate/SessionFactory">

        <property name="connection.datasource">java:comp/env/jdbc/mysql-upkeep</property>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="transaction.factory_class">
            net.sf.hibernate.transaction.JDBCTransactionFactory
        </property>

        <!-- Mapping files -->
        <mapping resource="com/jsoup/upkeep/core/Address.hbm.xml" />
        <mapping resource="com/jsoup/upkeep/core/DirectoryCategory.hbm.xml" />
        <mapping resource="com/jsoup/upkeep/core/DirectoryEntry.hbm.xml" />
        <mapping resource="com/jsoup/upkeep/core/UserAccount.hbm.xml" />

    </session-factory>


Mapping documents:

Code:
    <class name="UserAccount" table="ACCOUNT">
        <id name="id" type="long" unsaved-value="null">
            <column name="ACCOUNT_ID" not-null="true" unique="true"/>
            <generator class="native"/>
        </id>
        <property name="userId" column="USER_ID" not-null="true" unique="true" index="USER_ID_IDX"/>
        <property name="password" column="PASSWORD" not-null="true"/>
        <property name="contactName" column="CONTACT_NAME" not-null="true"/>
        <property name="email" column="EMAIL_ADDRESS" not-null="true" index="EMAIL_IDX" />
        <many-to-one name="mailingAddress" cascade="all">
            <column name="MAILING_ADDRESS_ID"/>
        </many-to-one>
        <many-to-one name="billingAddress" cascade="all">
            <column name="BILLING_ADDRESS_ID"/>
        </many-to-one>
        <property name="newsletterOk" column="NEWSLETTER_OK" not-null="true"/>
        <property name="thirdPartyOk" column="THIRD_PARTY_OK" not-null="true"/>
        <property name="agreedToTermsOn" column="AGREED_TO_TERMS_ON"/>
        <property name="status" column="STATUS_ID" type="com.jsoup.upkeep.core.AccountStatus"/>
        <property name="createdOn" column="CREATED_ON" not-null="true"/>
    </class>


Code between sessionFactory.openSession() and session.close():

Code:
public class AccountController {

    private static Log log = LogFactory.getLog(AccountController.class);

    private Session session = null;
    private Transaction tx = null;

    public AccountController() throws UpkeepException {
        initialize();
    }

    public UserAccount createAccount(UserAccount account) throws UpkeepException {
        log.debug("Trying to create account: " + account);

        if (account.getId() != null) {
            log.warn("UserAccount object passed in already has an ID");
        }

        try {
            // make sure user ID is not already taken
            List results = session.find("from UserAccount as ua where ua.userId = ?",
                                        account.getUserId(), Hibernate.STRING);
            log.debug("Found " + results.size() + " matches");

            if (results.size() == 0) {

                // create account
                account.setCreatedOn(new Date());
                session.save(account);
            } else {
                log.info("Failed to create account due to unavailable user ID "
                        + account);
                throw new UserIdUnavailableException();
            }
            tx.commit();
        } catch (HibernateException e) {
            log.error("Could not create user account", e);
            throw new UpkeepException("Failed to create account due to system error");
        } finally {
            closeAll();
        }
        log.info("Account created: " + account);

        return account;
    }

    public void recordAgreementWithTerms(Long accountId) throws UpkeepException {
        log.debug("Called recordAgreementWithTerms");

        try {
            // get user account and set agreed to terms to now
            UserAccount account = (UserAccount) session.get(UserAccount.class, accountId);
            if (account != null) {
                account.setAgreedToTermsOn(new Date());
                session.update(account);
                tx.commit();
            } else {
                log.warn("UserAccount " + accountId + " not found.");
            }
        } catch (HibernateException e) {
            log.error("Could not set agreed to terms date", e);
            throw new UpkeepException("Failed to set agreed to terms date due to system error.");
        } finally {
            closeAll();
        }
        log.info("Account " + accountId + " agreed to terms");
    }

    private void initialize() throws UpkeepException {
        try {
            if (session == null) {
                session = HibernateUtil.currentSession();
            }
            if (tx == null) {
                tx = session.beginTransaction();
            }
        } catch (HibernateException e) {
            log.error("Unable to get Hibernate session or start transaction");
            throw new UpkeepException("Problem initializing Hibernate");
        }
    }

    private void closeAll() {
        try {
            HibernateUtil.closeSession();
        } catch (HibernateException e) {
            log.warn("Unable to close Hibernate session", e);
        }
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what do you mean by "reload my webapp"

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 2:50 pm 
Newbie

Joined: Wed Aug 18, 2004 1:33 pm
Posts: 5
Resin will reload a webapp when you copy a new version of app.war to it's webapps directory. This is the same as shutting down the app and restarting. As soon as the app is shut down, the data disappears.

I think my problem is that the commit is not taking effect, but I am not sure why not, especially when I see the data through the external mysql command prompt. Maybe I should just switch to jBoss, or maybe there's a better way to configure Hibernate. I didn't find anything in the docs that addresses this particular problem, though, and I think I'm setting up transactions correctly.

Thanks for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 4:29 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you see changes using an "external" db client, that mean the commit is done...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 9:44 pm 
Newbie

Joined: Wed Aug 18, 2004 1:33 pm
Posts: 5
Ok, that confirms what I originally thought about the commit.

I deployed my app to JBoss with the same results. So it must be something with my Hibernate configuration. I imagine others are using MySQL without any trouble.

Hmmm.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 10:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Are your sure you don't have the automatic schema export enabled? Do you see the log output from Hibernate startup talking about (re)creating the tables?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 12:58 am 
Newbie

Joined: Wed Aug 18, 2004 1:33 pm
Posts: 5
Unless that's configured by default, no automatic schema export. There's nothing in the logs to that effect.

More testing reveals this fact: any row that is loaded by Hibernate gets deleted on application shutdown. I have a table that I populate with a script. I start the app, and the table gets loaded on first use (it's a look-up of categories). When the app shuts down, the data is gone. Is there some kind of auto-delete on Hibernate objects?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 6:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Make sure you don't have hbm2dll.auto=create-drop set


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 20, 2004 11:14 am 
Newbie

Joined: Wed Aug 18, 2004 1:33 pm
Posts: 5
I switched to HSQL (Hypersonic Database) and all is well. Must be a problem with MySQL (or at least the way I have MySQL configured).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.