Solved

Configuration Database connecton for CMP

Posted on 2006-11-19
12
401 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
ID: 17977423
first try deploying this example
http://www.kylev.com/code/jboss-mysql-and-cmp/
0
 
LVL 10

Expert Comment

by:mbvvsatish
ID: 17977429
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
ID: 17977436
send me what error u r getting??
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:arobe12
ID: 17977480
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
ID: 17977498
looks like a NullPointerException.
can you post the bean code along with you deployment descriptor xml's.
0
 

Author Comment

by:arobe12
ID: 17977514
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
 
LVL 10

Expert Comment

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

Author Comment

by:arobe12
ID: 17977558
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
ID: 17977627
>> <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
ID: 17977689
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
ID: 17977700
I see the problem. Thanks for your help.
0
 
LVL 10

Expert Comment

by:mbvvsatish
ID: 17977716
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

830 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