-->
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.  [ 6 posts ] 
Author Message
 Post subject: BMP EntityBean using Hibernate problem
PostPosted: Wed Jun 07, 2006 4:52 pm 
Newbie

Joined: Wed Jun 07, 2006 3:42 pm
Posts: 3
Quote:
Hi all,

I’m trying to use Hibernate inside a BMP Entity bean and using CMT to manage the transactions to the database.
The code below tries to persist data inside EntityBean::ejbStore, but it doesn’t seem to write to the database unless Session::flush() is called.
According to the documentation, hibernate should be flushing and committing data to the database when a JTA/CMT transaction commits.
What am I doing wrong?

Note:- I've tried XA database/drivers too.

Thanks,

Gavin

Code:
[Hibernate Config]
<hibernate-configuration>

  <session-factory name="boo_hoo">
   
   <property name="hibernate.connection.datasource">java:jdbc/mySql</property>
   <property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
   <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
   
   <!--
    <property name="jta.UserTransaction">java:comp/UserTransaction</property>
   <property name="current_session_context_class">jta</property>
   -->

   <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
   <!-- org.hibernate.transaction.WebSphereTransactionManagerLookup -->

    <property name="show_sql">true</property>
    <property name="debug">true</property>

    <mapping resource="Person.hbm.xml" />

  </session-factory>
 
</hibernate-configuration>

Code:
[Person.hbm.xml]
<hibernate-mapping>
  <class name=" PersonBean" table="person" dynamic-update="true" >
    <id name="id" column="id"></id>
    <property name="firstname" column="firstname" />
    <property name="surname" column="surname" />
  </class>
</hibernate-mapping>

Code:
public abstract class HibernateEntityBean implements EntityBean{

  protected EntityContext myEntityCtx;
  protected static SessionFactory sessionFactory;

  static{
    try{
      Configuration config = new Configuration();
      Configuration  inputConfig = config.configure("Hibernate.cfg.xml");
      sessionFactory = inputConfig.buildSessionFactory();
    }
    catch(Exception e){
      System.out.println(e);
    }
  }
  public void ejbLoad() {
    Serializable pk = (Serializable) getEntityContext().getPrimaryKey();
    Session session = getSession();
    session.load(this,pk);
  }
  public void ejbStore() {
    Session session = getSession();
    session.update(this);
    //session.flush(); Do I need this??!!
  }
  protected Session getSession(){
    Session session = sessionFactory.getCurrentSession();
    //session.setFlushMode(FlushMode.COMMIT);
    return session;
  }
  ...

Code:
[PersonBean]
public class PersonBean extends HibernateEntityBean{

  private String firstname;
  private String surname;
  private Integer id;

  public Integer ejbCreate(Integer key, String name1, String name2) throws javax.ejb.CreateException {
   
    id = key;
    firstname = name1;
    surname = name2;
   
    Session ses = getSession();
    ses.save(this);
    return key;
  }

  public Collection ejbFindByRange(Integer min,Integer max){

    Session ses = getSession();
    List result = ses.createQuery("select p.id from PersonBean p where p.id <= :max and p.id >= :min")
                     .setInteger("max",max.intValue())
                     .setInteger("min",min.intValue())
                     .list();
    return result;

  }
  public Integer ejbFindByPrimaryKey(Integer key){
    return key;
  }
  /* Getters and Setters here */
}



Quote:
Calling code inside a stateless session bean. I've tried CMT and BMT.
Note:- records 100-102 exist

Code:
  Collection col = ph.findByRange(new Integer(100),new Integer(102));
  Iterator it = col.iterator();
 
  while(it.hasNext()){
    Person p = (Person) it.next();
    p.setSurname(p.getSurname()+"a");
  }



Last edited by gavin1066 on Wed Jun 07, 2006 8:56 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 4:58 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
How is the deployment descriptor for your EJB set up? That specifies the transactional behaviour for the bean. It's possible you have a non-transactional bean. I believe there is some property for the transaction type which can have a value of "none" in addition to other values, such as "required". Maybe the container isn't creating a transaction because it doesn't think it has to.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:25 pm 
Newbie

Joined: Wed Jun 07, 2006 3:42 pm
Posts: 3
Quote:

Below is a snippet of the declared transaction demarcations.
I've also tried using BMT/UserTranaction with the session bean.





Code:
   
   <enterprise-beans>
      <entity>
         <ejb-name>Person</ejb-name>
         <home>PersonHome</home>
         <remote>Person</remote>
         <ejb-class>PersonBean</ejb-class>
         <persistence-type>Bean</persistence-type>
         <prim-key-class>java.lang.Integer</prim-key-class>
         <reentrant>true</reentrant>
      </entity>
      <session>
          <ejb-name>Session1</ejb-name>
         <home>Session1Home</home>
         <remote>Session1</remote>
         <ejb-class>Session1Bean</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Container</transaction-type>
         <reentrant>false</reentrant>
      </session>
   </enterprise-beans>

    <assembly-descriptor>
        <container-transaction>
            <method>
                <ejb-name>Session1</ejb-name>
                <method-name>*</method-name>
            </method>

            <trans-attribute>Required</trans-attribute>
        </container-transaction>
        <container-transaction>
            <method>
                <ejb-name>Person</ejb-name>
                <method-name>*</method-name>
            </method>

            <trans-attribute>Required</trans-attribute>
        </container-transaction>
    </assembly-descriptor>



Last edited by gavin1066 on Wed Jun 07, 2006 5:55 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:29 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
Have you enabled Hibernate DEBUG logging via log4j? That gives you detailed information about exactly what Hibernate is doing That might help you determine what is happening with regard to the JTA Transaction and Hibernate Session. This ought to be working.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:41 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
Which app server are you using? If it's WAS, try adding the following to your Hibernate configuration:

<property name="transaction.auto_close_session">true</property>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:44 pm 
Newbie

Joined: Wed Jun 07, 2006 3:42 pm
Posts: 3
I'm testing/developing in JBoss and hope to deploy in WebSphere.

I tried the property thing, didn't work.

I appreciate the suggestions though.


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