?
Solved

Configuration Database connecton for CMP

Posted on 2006-11-19
12
Medium Priority
?
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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