CMP bean not retrieving data from the Table

Discussions

EJB programming & troubleshooting: CMP bean not retrieving data from the Table

  1. CMP bean not retrieving data from the Table (3 messages)

    Hi,

    I am writing a simple CMP bean using weblogic 7.1 and MYSQL database.

    while writing a sample test application , the bean is able to write to the database table , but while retrieving uding the finder methods , it is giving a java.sql.SQLException stating that that no data is found . Please help as we are unable to proceed

    Thanks in advance , the stackTrace and code listing are given below


    The exact error is

    javax.ejb.FinderException: Exception in 'findByBasePrice' while using result set
    : '[email protected]'
    java.sql.SQLException: No data found
    java.sql.SQLException: No data found
            at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6207)
            at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3261)
            at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:
    5393)
            at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:321)

            at weblogic.jdbc.jts.ResultSet.getString(ResultSet.java:84)
            at weblogic.jdbc.rmi.internal.ResultSetImpl.getString(ResultSetImpl.java
    :178)
            at weblogic.jdbc.rmi.internal.ResultSetStraightReader.getString(ResultSe
    tStraightReader.java:37)
            at weblogic.jdbc.rmi.SerialResultSet.getString(SerialResultSet.java:128)

            at bank.ProductBean_meu6z3__WebLogic_CMP_RDBMS.__WL_loadGroup0FromRS(Pro
    ductBean_meu6z3__WebLogic_CMP_RDBMS.java:1242)
            at bank.ProductBean_meu6z3__WebLogic_CMP_RDBMS.ejbFindByBasePrice(Produc
    tBean_meu6z3__WebLogic_CMP_RDBMS.java:673)
            at java.lang.reflect.Method.invoke(Native Method)
            at weblogic.ejb20.cmp.rdbms.RDBMSPersistenceManager.collectionFinder(RDB
    MSPersistenceManager.java:314)
            at weblogic.ejb20.manager.BaseEntityManager.collectionFinder(BaseEntityM
    anager.java:739)
            at weblogic.ejb20.manager.BaseEntityManager.collectionFinder(BaseEntityM
    anager.java:712)
            at weblogic.ejb20.internal.EntityEJBHome.finder(EntityEJBHome.java:560)
            at bank.ProductBean_meu6z3_HomeImpl.findByBasePrice(ProductBean_meu6z3_H
    omeImpl.java:176)
            at bank.ProductBean_meu6z3_HomeImpl_WLSkel.invoke(Unknown Source)
            at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:362)
            at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerR
    ef.java:114)
            at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:313)
            at weblogic.security.service.SecurityServiceManager.runAs(SecurityServic
    eManager.java:785)
            at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.jav
    a:308)
            at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest
    .java:30)
            at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:153)
            at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:134)

            at weblogic.rmi.internal.BasicOutboundRequest.sendReceive(BasicOutboundR
    equest.java:109)
            at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
    ef.java:262)
            at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
    ef.java:229)
            at bank.ProductBean_meu6z3_HomeImpl_WLStub.findByBasePrice(Unknown Sourc
    e)
            at bank.Client.main(Client.java:39)




    ------------------------------------------------------------
              Client .java code is as follows
    ------------------------------------------------------------
    /*
     * Created on Aug 20, 2003
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package bank;
    import javax.naming.*;
    import javax.ejb.*;
    import javax.rmi.PortableRemoteObject;

    import java.util.*;
    /**
     * @author abhishek
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Client {

    public static void main(String[] args) throws Exception
    {
    ProductHome home = null;
    try
    {
    Properties prop = new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY , "weblogic.jndi.WLInitialContextFactory");
    prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
    Context ctx = new InitialContext(prop);
    Object obj = ctx.lookup("Product");
    home = (ProductHome)PortableRemoteObject.narrow(obj , ProductHome.class);
    System.out.println("Trying to create accounts .......");
    home.create("123-456-7890","P5-350","350 Mhz Pentium", 200);
    home.create("123-456-7891","P5-400","400 Mhz Pentium", 300);
    home.create("123-456-7892","P5-450","450 Mhz Pentium", 400);
    home.create("123-456-7893","sd-64","64 MB SD RAM", 50);
    home.create("123-456-7894","sd-128","128 MB SD RAM", 100);
    home.create("123-456-7895","sd-256","256 MB SD RAM", 200);
    Collection c = home.findByBasePrice(200);
    LinkedList l = new LinkedList(c);
    ListIterator li = l.listIterator();
    while(li.hasNext())
    {
    Product prod = (Product)PortableRemoteObject.narrow(li.next() , Product.class);
    System.out.println(prod.getDescription());
    }

    }catch(Exception e)
    {
    e.printStackTrace();

    }finally
    {
    LinkedList l =null;
    ListIterator li = null;
    if(home != null)
    {
    System.out.println("Destroying the Products");
    try
    {
    l = new LinkedList(home.findAllProducts());
    li = l.listIterator();
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    while(li.hasNext())
    {
    try
    {
    Product prod = (Product)PortableRemoteObject.narrow(li.next() , Product.class);
    prod.remove();
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    }
    }
    }
    ------------------------------------------------------------
                 Home Interface is
    ------------------------------------------------------------
    /*
     * Created on Aug 20, 2003
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package bank;
    import javax.ejb.*;
    import java.rmi.*;
    import java.util.Collection;
    /**
     * @author abhishek
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public interface ProductHome extends EJBHome{

    public Product create(String productID , String name , String description , double basePrice) throws CreateException , RemoteException;

    public Product findByPrimaryKey(String key) throws FinderException , RemoteException ;

    public Collection findByName(String name) throws FinderException , RemoteException;

    public Collection findByDescription(String description) throws FinderException , RemoteException;

    public Collection findByBasePrice(double Price) throws FinderException , RemoteException ;

    public Collection findExpensiveProducts(double price) throws FinderException , RemoteException;

    public Collection findCheapProducts(double price) throws FinderException , RemoteException;

    public Collection findAllProducts() throws FinderException , RemoteException ;

    }
    ------------------------------------------------------------
               the bean code is
    ------------------------------------------------------------
    /*
     * Created on Aug 20, 2003
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package bank;

    import javax.ejb.CreateException;
    import javax.ejb.EJBException;
    import javax.ejb.EntityBean;
    import javax.ejb.EntityContext;
    import javax.ejb.RemoveException;

    /**
     * @author abhishek
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public abstract class ProductBean implements EntityBean {

    public abstract void setProductId(String productId);
    public abstract void setName(String name);
    public abstract void setBasePrice(double price);
    public abstract void setDescription(String description);

    public abstract String getProductId();
    public abstract String getName();
    public abstract double getBasePrice();
    public abstract String getDescription();


    EntityContext ctx ;

    /* (non-Javadoc)
    * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
    */

    public void setEntityContext(EntityContext ctx)
    {
    this.ctx = ctx ;

    }

    public void unsetEntityContext()
    {
    this.ctx = null;
    }

    public String ejbCreate(String productId , String name , String description , double basePrice) throws CreateException
    {
    setProductId(productId);
    setName(name);
    setBasePrice(basePrice);
    setDescription(description);

    return productId;
    }

    public void ejbPostCreate(String productId , String name , String description , double basePrice)
    {}

    public void ejbRemove()
    {}

    public void ejbActivate()
    {}

    public void ejbPassivate()
    {}

    public void ejbLoad()
    {

    }

    public void ejbStore()
    {}

    }
    ------------------------------------------------------------
                   the ejb-jar.xml file is
    ------------------------------------------------------------
    <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'><ejb-jar>
    <enterprise-beans>
    <entity>
    <ejb-name>Product</ejb-name>
    <home>bank.ProductHome</home>
    <remote>bank.Product</remote>
    <ejb-class>bank.ProductBean</ejb-class>
    <persistence-type>Container</persistence-type>
    <prim-key-class>java.lang.String</prim-key-class>
    <reentrant>False</reentrant>
    <cmp-version>2.x</cmp-version>
    <abstract-schema-name>ProductBean</abstract-schema-name>
    <cmp-field>
    <field-name>productId</field-name>
    </cmp-field>
    <cmp-field>
    <field-name>name</field-name>
    </cmp-field>
    <cmp-field>
    <field-name>basePrice</field-name>
    </cmp-field>
    <cmp-field>
    <field-name>description</field-name>
    </cmp-field>
    <primkey-field>productId</primkey-field>

    <query>
    <query-method>
    <method-name>findByName</method-name>
    <method-params>
    <method-param>java.lang.String</method-param>
    </method-params>
    </query-method>
    <ejb-ql><![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.name = ?1]]></ejb-ql>
    </query>

    <query>
    <query-method>
    <method-name>findByDescription</method-name>
    <method-params>
    <method-param>java.lang.String</method-param>
    </method-params>
    </query-method>
    <ejb-ql><![CDATA[SELECT OBJECT(a) FROM ProductBean AS a where a.description = ?1]]></ejb-ql>
    </query>
    <query>
    <query-method>
    <method-name>findByBasePrice</method-name>
    <method-params>
    <method-param>double</method-param>
    </method-params>
    </query-method>
    <ejb-ql><![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.basePrice = ?1]]></ejb-ql>
    </query>
    <query>
    <query-method>
    <method-name>findExpensiveProducts</method-name>
    <method-params>
    <method-param>double</method-param>
    </method-params>
    </query-method>
    <ejb-ql><![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.basePrice > ?1 ]]></ejb-ql>
    </query>
    <query>
    <query-method>
    <method-name>findCheapProducts</method-name>
    <method-params>
    <method-param>double</method-param>
    </method-params>
    </query-method>
    <ejb-ql><![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.basePrice < ?1]]></ejb-ql>
    </query>
    <query>
    <query-method>
    <method-name>findAllProducts</method-name>
    <method-params></method-params>
    </query-method>
    <ejb-ql><![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.productId IS NOT NULL]]></ejb-ql>
    </query>
    </entity>
    </enterprise-beans>
    <assembly-descriptor>
    <container-transaction>
    <method>
    <ejb-name>Product</ejb-name>
    <method-name>*</method-name>
    </method>
    <trans-attribute>Required</trans-attribute>
    </container-transaction>
    </assembly-descriptor>
    </ejb-jar>
    ------------------------------------------------------------
                         weblogic-ejb-jar.xml is
    ------------------------------------------------------------
    <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd'>
    <weblogic-ejb-jar>
    <weblogic-enterprise-bean>
    <ejb-name>Product</ejb-name>
    <entity-descriptor>
    <pool></pool>
    <entity-cache></entity-cache>
    <persistence>
    <persistence-use>
    <type-identifier>WebLogic_CMP_RDBMS</type-identifier>
    <type-version>7.0</type-version>
    <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
    </persistence-use>
    </persistence>
    <entity-clustering></entity-clustering>
    </entity-descriptor>
    <transaction-descriptor></transaction-descriptor>
    <jndi-name>Product</jndi-name>
    </weblogic-enterprise-bean>
    </weblogic-ejb-jar>

    -----------------------------------------------------------
           weblogic-rdbms-jar.xml file is
    -----------------------------------------------------------
    <!DOCTYPE weblogic-rdbms-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB RDBMS Persistence//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-rdbms20-persistence-700.dtd'>
    <weblogic-rdbms-jar>
    <weblogic-rdbms-bean>
    <ejb-name>Product</ejb-name>
    <data-source-name>examples-dataSource-ejbPoolCMP</data-source-name>
    <table-map>
    <table-name>product</table-name>
    <field-map>
    <cmp-field>productId</cmp-field>
    <dbms-column>productId</dbms-column>
    </field-map>
    <field-map>
    <cmp-field>name</cmp-field>
    <dbms-column>name</dbms-column>
    </field-map>
    <field-map>
    <cmp-field>basePrice</cmp-field>
    <dbms-column>basePrice</dbms-column>
    </field-map>
    <field-map>
    <cmp-field>description</cmp-field>
    <dbms-column>description</dbms-column>
    </field-map>
    </table-map>
    </weblogic-rdbms-bean>
    <create-default-dbms-tables>True</create-default-dbms-tables>
    </weblogic-rdbms-jar>

    ------------------------------------------------------------
  2. I don't know for certain if this will solve your problem, but I have encountered a similar situation. If you insert a row and attempt to retrieve that row in the same transaction through WebLogic CMP, WebLogic CMP will not return the row unless you include <include-updates>True</include-updates> in weblogic-cmp-rdbms-jar.xml under <weblogic-rdbms-bean>. The default is False and provides the best performance but this setting does not conform to the EJB spec. Setting this to True will cause WebLogic CMP to flush updates made in the transaction to disk before the finder query is executed, this will cause the updates to be visible in the transaction.

    See http://e-docs.bea.com/wls/docs70/ejb/EJB_reference.html#1080000
  3. Hi thanks a lot for the response , However i am still getiing the same error after making the changes to weblogic-cmp-rdbms-jar.xml . The Modified version is

    <!DOCTYPE weblogic-rdbms-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB RDBMS Persistence//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-rdbms20-persistence-700.dtd'>
    <weblogic-rdbms-jar>
    <weblogic-rdbms-bean>
    <ejb-name>Product</ejb-name>
    <data-source-name>examples-dataSource-ejbPoolCMP</data-source-name>
    <table-map>
    <table-name>product</table-name>
    <field-map>
    <cmp-field>productId</cmp-field>
    <dbms-column>productId</dbms-column>
    </field-map>
    <field-map>
    <cmp-field>name</cmp-field>
    <dbms-column>name</dbms-column>
    </field-map>
    <field-map>
    <cmp-field>basePrice</cmp-field>
    <dbms-column>basePrice</dbms-column>
    </field-map>
    <field-map>
    <cmp-field>description</cmp-field>
    <dbms-column>description</dbms-column>
    </field-map>
    </table-map>
    <weblogic-query>
    <query-method>
    <method-name>findByBasePrice</method-name>
    <method-params></method-params>
    </query-method>
    <include-updates>True</include-updates>
    </weblogic-query>
    </weblogic-rdbms-bean>
    <create-default-dbms-tables>True</create-default-dbms-tables>
    </weblogic-rdbms-jar>


    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    I have also tried to populate the data manually in the databse and then tring to query it using the CMP bean , however i am still getting the same error . In this scenario i presume , that no common transaction should come in the picture .
    One more thought , has it got anything to do with MySQL database because, i am getting the same error in weblogic supplied examples
    Any other suggestions , any tip/hint would be highly apreciated ....
  4. I'm no expert, but perhaps its because you are relying on equality between 2 doubles. And perhaps one is not exactly as precise as the other?

    Could it be that you may need to add a little tolerance to your double somehow?

    For example something similar to:
    [code]
    SELECT OBJECT(a) FROM ProductBean AS a WHERE a.basePrice BETWEEN (?1 - 0.1) and (?1 + 0.1)
    [/code]
    Of course, this may not be the problem, but it might be worth just checking?

    Cheers, Neil