This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes bmp is throwing exception in weblogic 8.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "bmp is throwing exception in weblogic 8.1" Watch "bmp is throwing exception in weblogic 8.1" New topic
Author

bmp is throwing exception in weblogic 8.1

Abhishek Srivastava
Greenhorn

Joined: Sep 29, 2005
Posts: 6
Hello EveryBody

I tried to execute BMP over weblogic 8.1 but a simple mistake cause an exception,plz try to solve it.

i used table in Oracle as savingsaccount with following fields :

id varchar(3) primary key,
firstname varchar(24),
lastname varchar(24),
balance numeric(10,2));

I made connection pool as :

connectionpool : saving
datasource name : saving
jndi-name : saving

Now

1. my bmp remote is :


public interface SavingsAccount extends EJBObject {

public void debit(BigDecimal amount)
throws InsufficientBalanceException, RemoteException;

public void credit(BigDecimal amount)
throws RemoteException;

public String getFirstName()
throws RemoteException;

public String getLastName()
throws RemoteException;

public BigDecimal getBalance()
throws RemoteException;
}


2. my Bmp home is :


public interface SavingsAccountHome extends EJBHome {

public SavingsAccount create(String id, String firstName,
String lastName, BigDecimal balance)
throws RemoteException, CreateException;

public SavingsAccount findByPrimaryKey(String id)
throws FinderException, RemoteException;

public Collection findByLastName(String lastName)
throws FinderException, RemoteException;

public Collection findInRange(BigDecimal low,
BigDecimal high)
throws FinderException, RemoteException;

public void chargeForLowBalance(BigDecimal minimumBalance,
BigDecimal charge)
throws InsufficientBalanceException,RemoteException;
}

3. My bean class as :

import java.sql.*;
import javax.sql.DataSource;
import java.util.*;
import java.math.*;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
public class SavingAccountBean implements EntityBean

{
private String id;
private String firstName;
private String lastName;
private BigDecimal balance;
private EntityContext context;
private Connection con;
private String dbName="saving";

public void debit(BigDecimal amount)
throws InsufficientBalanceException, RemoteException{

/* if (balance.compareTo(amount) == -1) {
throw new Exception();
} */
balance = balance.subtract(amount);
}
public void credit(BigDecimal amount) {

balance = balance.add(amount);
}
public String getFirstName() {

return firstName;
}
public String getLastName() {

return lastName;
}

public BigDecimal getBalance() {

return balance;
}

public void ejbHomeChargeForLowBalance(BigDecimal minimumBalance, BigDecimal charge)
throws Exception {

try {

SavingsAccountHome home =
(SavingsAccountHome)context.getEJBHome();
Collection c = home.findInRange(new BigDecimal("0.00"),
minimumBalance.subtract(new BigDecimal("0.01")));

Iterator i = c.iterator();

while (i.hasNext()) {
SavingsAccount account = (SavingsAccount)i.next();
if (account.getBalance().compareTo(charge) == 1) {
account.debit(charge);
}
}

} catch (Exception ex) {
throw new EJBException("ejbHomeChargeForLowBalance: "
+ ex.getMessage());
}
}


public String ejbCreate(String id, String firstName,String lastName, BigDecimal balance) throws CreateException {

if (balance.signum() == -1) {
throw new CreateException ("A negative initial balance is not allowed.");
}

try {
insertRow(id, firstName, lastName, balance);
} catch (Exception ex) {
throw new EJBException("ejbCreate: " +
ex.getMessage());
}

this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;

return id;
}

public String ejbFindByPrimaryKey(String primaryKey)
throws FinderException {

boolean result;

try {
result = selectByPrimaryKey(primaryKey);
} catch (Exception ex) {
throw new EJBException("ejbFindByPrimaryKey: " +
ex.getMessage());
}

if (result) {
return primaryKey;
}
else {
throw new ObjectNotFoundException
("Row for id " + primaryKey + " not found.");
}
}
public Collection ejbFindByLastName(String lastName)
throws FinderException {

Collection result;

try {
result = selectByLastName(lastName);
} catch (Exception ex) {
throw new EJBException("ejbFindByLastName " +
ex.getMessage());
}
return result;
}
public Collection ejbFindInRange(BigDecimal low, BigDecimal high) throws FinderException
{
Collection result;
try
{
result=selectInRange(low,high);
}
catch(Exception e)
{
throw new EJBException("ejbFindInRange:"+e.getMessage());
}

return result;
}

public void ejbRemove() {
try {
deleteRow(id);
}
catch (Exception ex) {
throw new EJBException("ejbRemove: " +
ex.getMessage());
}
}
public void setEntityContext(EntityContext context)
{
this.context=context;
try
{
makeConnection();
}
catch(Exception e)
{
throw new EJBException("Unable to connect to database. "+e.getMessage());
}
}
public void unsetEntityContext()
{
try
{
con.close();
}
catch(SQLException ex)
{
throw new EJBException("unSetEntityContext:"+ex.getMessage());

}
}
public void ejbActivate()
{
id=(String)context.getPrimaryKey();
}
public void ejbPassivate()
{
id=null;
}

public void ejbLoad() {

try {
loadRow();
} catch (Exception ex) {
throw new EJBException("ejbLoad: " +
ex.getMessage());
}
}

public void ejbStore() {

try {
storeRow();
} catch (Exception ex) {
throw new EJBException("ejbStore: " +
ex.getMessage());
}
}
public void ejbPostCreate(String id, String firstName,String lastName,BigDecimal balance)
{

}
private void makeConnection() throws NamingException,SQLException
{
InitialContext ic=new InitialContext();
DataSource ds=(DataSource)ic.lookup(dbName);
con=ds.getConnection();

}
private void insertRow(String id,String firstname, String lastname,BigDecimal balance) throws SQLException
{
String insertStatement ="insert into savingsaccount values(?,?,?,?)";
PreparedStatement preStmt=con.prepareStatement(insertStatement);
preStmt.setString(1,id);
preStmt.setString(2,firstName);
preStmt.setString(3,lastName);
preStmt.setBigDecimal(4,balance);
preStmt.executeUpdate();
preStmt.close();

}
private void deleteRow(String id) throws SQLException
{
String deleteStatement="delete from savingsaccount where id=?";
PreparedStatement preStmt=con.prepareStatement(deleteStatement);
preStmt.setString(1,id);
preStmt.executeUpdate();
preStmt.close();

}
private boolean selectByPrimaryKey(String primaryKey) throws SQLException
{
String selectStatement ="select id"+"from savingsaccount where id=?";
PreparedStatement preStmt=con.prepareStatement(selectStatement);
preStmt.setString(1,primaryKey);
ResultSet rs=preStmt.executeQuery();
ArrayList a= new ArrayList();
boolean result=rs.next();
preStmt.close();
return result;
}
private Collection selectByLastName(String lastName) throws SQLException
{
String selectStatement ="select id"+"from savingsaccount where lastname=?";
PreparedStatement preStmt=con.prepareStatement(selectStatement);
preStmt.setString(1,lastName);
ResultSet rs=preStmt.executeQuery();
ArrayList a= new ArrayList();
while(rs.next())
{
String id=rs.getString(1);
a.add(id);
}
preStmt.close();
return a;
}
private Collection selectInRange(BigDecimal low,BigDecimal high) throws SQLException {

String selectStatement="select id from savingsaccount"+"where balance between ? and ?";
PreparedStatement prepStmt=con.prepareStatement(selectStatement);
prepStmt.setBigDecimal(1,low);
prepStmt.setBigDecimal(2,high);
ResultSet rs=prepStmt.executeQuery();
ArrayList a=new ArrayList();
while(rs.next())
{
String id=rs.getString(1);
a.add(id);

}
prepStmt.close();
return a;
}
private void loadRow() throws SQLException
{
String selectStatement="select firstname,lastname,balance"+"from savingsaccount where id=?";
PreparedStatement prepStmt=con.prepareStatement(selectStatement);
prepStmt.setString(1,this.id);
ResultSet rs=prepStmt.executeQuery();
if(rs.next())
{
this.firstName=rs.getString(1);
this.lastName=rs.getString(2);
this.balance=rs.getBigDecimal(3);
prepStmt.close();
}
else
{
prepStmt.close();
throw new NoSuchEntityException("Row for id "+id+"no+ found in database.");

}
}
private void storeRow() throws SQLException
{
String updateStatement= "update savingsaccount set firstname=?,"+"lastname=?,balance=? "+"where id=?";
PreparedStatement preStmt=con.prepareStatement(updateStatement);

preStmt.setString(1,firstName);
preStmt.setString(2,lastName);
preStmt.setBigDecimal(3,balance);
preStmt.setString(4,id);
int rowcount=preStmt.executeUpdate();
preStmt.close();
if(rowcount==0)
{
throw new EJBException("Storing row for id"+id+"failed");

}
}
}

4. Now I put ejb-jar in META_INF as :

<?xml version="1.0"?>

<!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>bmp</ejb-name>
<home>SavingsAccountHome</home>
<remote>SavingsAccount</remote>
<ejb-class>SavingAccountBean</ejb-class>
<persistence-type>Bean</persistence-type>
<!-- <prim-key-class>java.lang.String</prim-key-class> -->
<prim-key-class>ItemKey</prim-key-class>
<reentrant>False</reentrant>
<resource-ref>
<res-ref-name>saving</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>bmp</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

</ejb-jar>

5. Also i placed weblogic-ejb-jar.xml in META-INF

<?xml version="1.0"?>

<!DOCTYPE weblogic-ejb-jar PUBLIC
'-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN'
'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'>

<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>bmp</ejb-name>

<reference-descriptor>
<resource-description>
<res-ref-name>saving</res-ref-name>
<jndi-name>saving</jndi-name>
</resource-description>
</reference-descriptor>

<jndi-name>abhi</jndi-name>

</weblogic-enterprise-bean>

</weblogic-ejb-jar>


Now i made bmp.jar and put inside EJBContainer
and I run my client as :

public class SavingAccountClient
{
public static void main(String s[])
{
try
{
Properties prop=new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL,"t3://localhost:7001");

Context initial=new InitialContext(prop);
Object objref=initial.lookup("abhi");
SavingsAccountHome home=(SavingsAccountHome)PortableRemoteObject.narrow(objref,SavingsAccountHome.class);


BigDecimal zeroAmount = new BigDecimal("0.00");
SavingsAccount duke=home.create("125","neha","Earl",zeroAmount);
duke.credit(new BigDecimal("88.50"));
duke.debit(new BigDecimal("20.25"));
BigDecimal balance=duke.getBalance();
System.out.println("balance="+balance);
duke.remove();

SavingsAccount joe=home.create("836","Joe","Jones",zeroAmount);
joe.credit(new BigDecimal("34.55"));
SavingsAccount jones=home.findByPrimaryKey("836");

jones.debit(new BigDecimal("2.00"));
balance=jones.getBalance();
System.out.println("balance="+balance);
SavingsAccount pat=home.create("730","John","Smith",zeroAmount);
pat.credit(new BigDecimal("19.54"));
SavingsAccount mary=home.create("268","Mary","Smith",zeroAmount);
mary.credit(new BigDecimal("100.07"));
Collection c=home.findByLastName("Smith");
Iterator i=c.iterator();
while(i.hasNext())
{
SavingsAccount account=(SavingsAccount)i.next();
String id=(String)account.getPrimaryKey();
BigDecimal amount=account.getBalance();
System.out.println(id+"; "+amount);
}
c=home.findInRange(new BigDecimal("20.00"),new BigDecimal("99.00"));
i=c.iterator();
while(i.hasNext())
{
SavingsAccount account=(SavingsAccount)i.next();
String id=(String)account.getPrimaryKey();
BigDecimal amount=account.getBalance();
System.out.println(id+"; "+amount);
}
SavingsAccount pete=home.create("904","Pete","Carlson",new BigDecimal("5.00"));
SavingsAccount sally=home.create("905","Sally","Fortney",new BigDecimal("8.00"));
home.chargeForLowBalance(new BigDecimal("10.00"),new BigDecimal("1.00"));
BigDecimal reducedAmount=pete.getBalance();
reducedAmount=sally.getBalance();
System.out.println(reducedAmount);
System.exit(0);

}
catch(NamingException ex)
{
System.err.println("Record Inserted"+ex.toString());
//ex.printStackTrace();

} catch(Exception ex)
{
System.err.println("OtherException");
//ex.printStackTrace();

}
}

}

and thus server throws following exception as :


weblogic.ejb20.compliance.ComplianceException: In EJB bmp, the finder ejbFindByP
rimaryKey(java.lang.String) returned an unexpected type. Finders in the bean cla
ss must return the primary key type, java.util.Collection, or java.util.Enumerat
ion

please try to sort out this problem,where i made it wrong.

thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"Mr. Java srivastava"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
jQuery in Action, 2nd edition
 
subject: bmp is throwing exception in weblogic 8.1
 
Similar Threads
EJB unable to find my Home interface
EJB problem
jboss - mysql Table not found in statement
weblogic ejb
help me! stateful session bean problem