facebook

BasicDB Demo is not saving new rows in mySql table?

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #267184 Reply

    Kent Johnson
    Participant

    I followed the BasicDB demo on my pc and everything seems to work except when I enter new rows they don’t apear in mySql database? No exceptions are thrown and no errors. The newly added row shows up on the jsp page but not in the mySql database?

    This is the code for adding new data:

    /**
    * addItem() inserts new <code>Item</code> into the database through Hibernate.
    *
    * @param item A new <code>Item</code> to be added.
    */
    public void addVipdata(Vipdata data)
    {
    Session session = null;

    try
    {
    session = SessionFactory.currentSession();
    session.save(data);
    session.flush();
    }
    catch (HibernateException e)
    {
    System.err.println(“Hibernate Exception” + e.getMessage());
    throw new RuntimeException(e);
    }
    /*
    * Regardless of whether the above processing resulted in an Exception
    * or proceeded normally, we want to close the Hibernate session. When
    * closing the session, we must allow for the possibility of a Hibernate
    * Exception.
    *
    */
    finally
    {
    if (session != null)
    {
    try
    {
    session.close();
    }
    catch (HibernateException e)
    {
    System.err.println(“Hibernate Exception” + e.getMessage());
    throw new RuntimeException(e);
    }

    }
    }
    }

    It looks like it is executing the following to add a row

    >>session = SessionFactory.currentSession();
    >>session.save(data);
    >>session.flush();

    Is this code correct or is there another reason my rows are not being inserted in to the mySql database?

    I’m not very familiar with the Hibernate calls and I have some questions on how flush(), commit() ext. work
    1. Does the flush() act like a commit for a transaction? Or should I open a transaction then issue a commit to force the row to the database?
    2. I dont see any transaction statements in any of the code for BasicDB. Should I add transactions or will a flush() work to force the row to the database?

    Thanks!
    Kent
    kwjohn@xmission.com

    #267212 Reply

    Riyad Kalla
    Member

    Kent,
    You need to create a transaction from the session, begin it, do your save, then commit the transaction. Then you should be all set.

    #267228 Reply

    Haris Peco
    Member

    Kent,

    Flush send commands to database server, but don’t commit it.You have to do dml commands (update,delete,insert) in transaction.It is recommended that you do quering (select) in transaction as well (this is not necessary for database like oracle, but you can do it without issues).It is important that you call transaction.rollback() if commit doesn’t success and you can open new session after that.
    Usually pattern for transaction commands is :

    Session session = null;
    Transaction transaction = null;
    try {
    session = SessionFactory.currentSession();
    transaction = session.beginTransaction();
    session.save(object); // or what ever you do
    transaction.commit();
    catch (Exception e) {
    if (transaction != null) {
    try {
    transaction.rollback();
    } catch (Exception e) {}
    }
    } finally {
    if (session != null) {
    try { session.close(); } catch (Exception e) {}
    }
    }

    Regards,

    #267313 Reply

    Kent Johnson
    Participant

    Thanks!

    I will try a transaction and see if that works for me.

    Kent

    #267480 Reply

    If transactions are needed to save the data — then perhaps the hibernate generation templates should be updated to ahve transaction code.

    Lee

    #267499 Reply

    Haris Peco
    Member

    Lee,

    If transactions are needed to save the data — then perhaps the hibernate generation templates should be updated to ahve transaction code.

    It is not simple.We can’t know when you want transaction. For example, you can want save two objects in one transaction (this is usually transaction case).If we add transaction in saving for every object you can got transaction break – one object is saved and other is not.
    There is different ways for declarative transaction boundary using spring, application servers etc and you can use something if you want (spring declarative transaction works great with hibernate).Anyway, you decide when transaction start and when commit/rollback.

    Regards,

    #267506 Reply

    @support-snpe wrote:

    There is different ways for declarative transaction boundary using spring, application servers etc and you can use something if you want (spring declarative transaction works great with hibernate).Anyway, you decide when transaction start and when commit/rollback.

    I can understand that. I can also understand that one would expect the code generated would work. It doesn’t. For someone who understands the technology well, the answer is simple. For someone who’s using MyEclipse to generate code for them because they DON’T know how (yet) to write it themself — it’s quite frustrating.

    At a minimum — the generated code could contain comments: // This “save” method will not actually save data until you put in code for a transaction

    Or — you can put the transaction code in so that it works — and comment that says: // remove transaction code for scenarios where this save is part of a larger context

    Something. Generating code that doesn’t work — well, that’s just not a good default –IMHO.

    Lee

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: BasicDB Demo is not saving new rows in mySql table?

You must be logged in to post in the forum log in