-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate data sync between 2 sessioin
PostPosted: Sun Sep 18, 2011 2:47 am 
Newbie

Joined: Sun Sep 18, 2011 2:15 am
Posts: 2
Hi, Im new to Hibernate and trying to set up some demo. Here is my code:
Pojo file:
Code:
public class Tblgeneric  implements java.io.Serializable {
     private Integer id;
     private String description;
     private String url;
     private String title;
    public Tblgeneric() {
    }

    public Tblgeneric(String title) {
        this.title = title;
    }
    public Tblgeneric(String description, String url, String title) {
       this.description = description;
       this.url = url;
       this.title = title;
    }
   
    public Integer getId() {
        return this.id;
    }
   
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDescription() {
        return this.description;
    }
   
    public void setDescription(String description) {
        this.description = description;
    }
    public String getUrl() {
        return this.url;
    }
   
    public void setUrl(String url) {
        this.url = url;
    }
    public String getTitle() {
        return this.title;
    }
   
    public void setTitle(String title) {
        this.title = title;
    }
}

Hibernate Util:
Code:
public class HibernateUtil {
    private static final SessionFactory sessionFactory;
   
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml)
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception.
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
   
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Application file: this is a demo so I did not split my app into layers:
Code:
public class AppServlet extends Application implements Button.ClickListener{
   
    private class asdf{
        private int q = 0;
        public int getQ(){
            ++q;
            return q;
        }
    }
    private static asdf ASDF;

//    public static Session s = HibernateUtil.getSessionFactory().getCurrentSession();
    TextField tf;
    private static int i = 0;
    @Override
    public void init() {
        Window mainWindow = new Window();
        mainWindow.setSizeFull();
        setMainWindow(mainWindow);
        Button a = new Button("Alert", this);
        mainWindow.addComponent(a);
        Button b = new Button("Add", this);
        mainWindow.addComponent(b);
        Button _u = new Button("Update", this);
        mainWindow.addComponent(_u);
        tf = new TextField();
        mainWindow.addComponent(tf);
        Button c = new Button("Show", this);
        mainWindow.addComponent(c);
    }
   
    private Tbluser u;

    @Override
    public void buttonClick(ClickEvent event) {
        Button b = event.getButton();
        if(b.getCaption().equals("Alert")){
            if(ASDF == null){
                ASDF = new asdf();
            }
            getMainWindow().showNotification(ASDF.getQ() + "");
            getMainWindow().addComponent(new Label(HibernateUtil.getSessionFactory().getCurrentSession().toString()));
        }else if(b.getCaption().equals("Add")){
           
            Tblgeneric generic = new Tblgeneric(" test 1234 ", " teset ", " haha ");
//            Transaction t = s.getTransaction();
            Transaction t = HibernateUtil.getSessionFactory().getCurrentSession().getTransaction();
            t.begin();
            Criteria c = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Tbluser.class);
//            Criteria c = s.createCriteria(Tbluser.class);
            u = (Tbluser) c.list().get(0);
            HibernateUtil.getSessionFactory().getCurrentSession().saveOrUpdate(generic);
//            s.saveOrUpdate(generic);
            t.commit();
//            s.close();
        }else if(b.getCaption().equals("Show") ){
//            Transaction t = s.getTransaction();
            Transaction t = HibernateUtil.getSessionFactory().getCurrentSession().getTransaction();
            t.begin();
//            Criteria c = s.createCriteria(Tblgeneric.class);
            Criteria c = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Tblgeneric.class);
            List<Tblgeneric> list = c.list();
            for(Tblgeneric g : list){
                Label l = new Label(g.getId() + " " + g.getDescription() + g.getTitle() + g.getUrl());
                getMainWindow().addComponent(l);
            }
            t.commit();
//            s.close();
        }else{
            getMainWindow().addComponent(new Label(u.getTblrole().getName()));
//            Transaction t = s.getTransaction();
            Transaction t = HibernateUtil.getSessionFactory().getCurrentSession().getTransaction();
            t.begin();
            Criteria c = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Tblgeneric.class).add(Restrictions.eq("id", Integer.parseInt(tf.getValue().toString())));
//            Criteria c = s.createCriteria(Tblgeneric.class).add(Restrictions.eq("id", Integer.parseInt(tf.getValue().toString())));
            Tblgeneric g = (Tblgeneric) c.list().get(0);
            g.setDescription(g.getId().toString());
            HibernateUtil.getSessionFactory().getCurrentSession().saveOrUpdate(g);
//            s.saveOrUpdate(g);
            t.commit();
//            s.close();
        }
    }

}

When I use this code and open two browser(Chrome and FF), and click the "Add" button, both session get the new record. But when I click "Update" in one session (in FF for example), the other session wont show the updated result, just the row with old data, although in both database and session in firefox show the updated row.
I think it's because of Hibernate cache, and I did some reading. My first try was to use session.clear(), which will end up force hibernate to retrieve new data. But if I store one entity on my presentation layer, and it has relation mapping, this will end up throw lazy initiation exception. I can re merge this entity, but if I have like 10 entity, it's quite inconvenience.
Second thing I came to was the thread http://community.jboss.org/wiki/OpenSessionInView. But I dont want to use JTA since I will have to use a shared server and they won't let me do anything.
this is my config file:
Code:
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ICTED</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">12345</property>
    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="hibernate.current_session_context_class">managed</property>

And I use the filter as showed in that thread. But same thing happened.
I tried to set my Session to static as I commented out in the code of my application file, and it show me the correct result, I think because every instance of my application used the same Session, so when ever I update my data, it show the correct result.
I know I can manage this with Spring, but I don't have time to read yet.
So can anybody give me a solution for this problem, and what's the risk setting Session to static and use one session for every instance of my application?
Thanks very much.


Top
 Profile  
 
 Post subject: Re: Hibernate data sync between 2 sessioin
PostPosted: Tue Sep 20, 2011 11:28 pm 
Newbie

Joined: Sun Sep 18, 2011 2:15 am
Posts: 2
Any one, please help?


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