Solved

Configuration Database connecton for CMP

Posted on 2006-11-19
12
398 Views
Last Modified: 2013-11-24
I'm newbie in EJB. Now I'm writing an EJB project. I use JBuilder 2006 and JBoss 4.0 for writing my project with CMP Entity Bean. I try many time but can't connect to database. I use Mysql database, connected by MySql JDBC over DataSource. Pls help me, is there need any configuraton?
0
Comment
Question by:arobe12
  • 6
  • 5
12 Comments
 
LVL 10

Expert Comment

by:mbvvsatish
Comment Utility
first try deploying this example
http://www.kylev.com/code/jboss-mysql-and-cmp/
0
 
LVL 10

Expert Comment

by:mbvvsatish
Comment Utility
here is how you set up a mysql datasource in jboss
http://wiki.jboss.org/wiki/Wiki.jsp?page=SetUpAMysqlDatasource
0
 
LVL 5

Expert Comment

by:deeppra
Comment Utility
send me what error u r getting??
0
 

Author Comment

by:arobe12
Comment Utility
I can lookup my EJB, but can't connect to database, here is error message that apperes when I run Client test Bean:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
      java.rmi.ServerException: RuntimeException; nested exception is:
      java.lang.IllegalStateException: createBeanClassInstanceCommand == null
0
 
LVL 10

Expert Comment

by:mbvvsatish
Comment Utility
looks like a NullPointerException.
can you post the bean code along with you deployment descriptor xml's.
0
 

Author Comment

by:arobe12
Comment Utility
This is Bean Code

//////////////////////////////////

package testproject;

import java.sql.*;
import java.util.Properties;
import java.util.Enumeration;
import java.util.Vector;
import java.rmi.RemoteException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

public class CMPProductBean implements EntityBean {

  EntityContext context;

  public String productId;
  public String productName;
  public String description;
  public double price;

  public String getProductId() {
    System.out.println("getProductId");
    return productId;
  }

  public String getProductName() {
    System.out.println("getProductName");
    return productName;
  }

  public String getDescription() {
    System.out.println("getDescription");
    return description;
  }
  public double getPrice() {
    System.out.println("getPrice");
    return price;
  }

  public String ejbCreate(int productId, String productName,
    String description, double price)
    throws RemoteException, CreateException {
    System.out.println("ejbCreate");
    this.productId = Integer.toString( productId);
    this.productName = productName;
    this.description = description;
    this.price = price;
    return Integer.toString(productId);
  }

  public void ejbPostCreate(int productId, String productName,
    String description, double price)
    throws RemoteException, CreateException {
    System.out.println("ejbPostCreate");
  }

  public void ejbRemove() throws RemoteException {
    System.out.println("ejbRemove");
  }

  public void ejbActivate() {
    System.out.println("ejbActivate");
  }

  public void ejbPassivate() {
    System.out.println("ejbPassivate");
  }

  public void ejbLoad() {
    System.out.println("ejbLoad");
  }
  public void ejbStore() {
    System.out.println("ejbStore");
  }
  public void setEntityContext(EntityContext context) {
    System.out.println("setEntityContext");
    this.context = context;

  }

  public void unsetEntityContext() {
    System.out.println("unsetEntityContext");
    context = null;
  }
}

/////////////////////////////////////////////////////////////////

This is xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">

<ejb-jar>
  <display-name>HelloCMP</display-name>
  <enterprise-beans>
    <entity>
      <ejb-name>CMPProduct</ejb-name>
      <home>testproject.CMPProductHome</home>
      <remote>testproject.CMPProduct</remote>
      <ejb-class>testproject.CMPProductBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-field>
        <field-name>productId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>productName</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>description</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>price</field-name>
      </cmp-field>
      <primkey-field>productId</primkey-field>
      <resource-ref>
        <res-ref-name>jdbc/DataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    </entity>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>CMPProduct</ejb-name>
        <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Expert Comment

by:mbvvsatish
Comment Utility
i am looking for jbosscmp-jdbc.xml please post it here
0
 

Author Comment

by:arobe12
Comment Utility
Here is jbosscmp-jdbc.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 4.0//EN" "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">

<jbosscmp-jdbc>
  <defaults>
    <datasource>DataSource</datasource>
    <datasource-mapping>Hypersonic SQL</datasource-mapping>
  </defaults>
  <enterprise-beans>
    <entity>
      <ejb-name>CMPProduct</ejb-name>
      <table-name>product</table-name>
      <cmp-field>
        <field-name>productId</field-name>
        <column-name>PRODUCT_ID</column-name>
      </cmp-field>
      <cmp-field>
        <field-name>productName</field-name>
        <column-name>PRODUCT_NAME</column-name>
      </cmp-field>
      <cmp-field>
        <field-name>description</field-name>
        <column-name>DESCRIPTION</column-name>
      </cmp-field>
      <cmp-field>
        <field-name>price</field-name>
        <column-name>PRICE</column-name>
      </cmp-field>
    </entity>
  </enterprise-beans>
</jbosscmp-jdbc>

0
 
LVL 10

Accepted Solution

by:
mbvvsatish earned 500 total points
Comment Utility
>> <datasource>DataSource</datasource>
>> <datasource-mapping>Hypersonic SQL</datasource-mapping>

change the above lines in jbosscmp-jdbc.xml to the following

<datasource>java:/DataSource</datasource>
<datasource-mapping>MySql</datasource-mapping>
where DataSource is the name of the datasource that you created for mysql
0
 

Author Comment

by:arobe12
Comment Utility
Succeful but it appears new error:

javax.ejb.CreateException: Error checking if entity exists:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'PRODUCT_ID' in 'where clause'
0
 

Author Comment

by:arobe12
Comment Utility
I see the problem. Thanks for your help.
0
 
LVL 10

Expert Comment

by:mbvvsatish
Comment Utility
check whether you have declared the 'PRODUCT_ID' as an auto_increment column in the mysql database.
if so you need to change your deployment descriptors.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now