I have written a very simple stateful session bean that inserts records into
InstantDB.  If I have only 1 client inserting records, I experience to
problems.  Once I start getting multiple client, I experience random problems.

The following are the 3 most common errors:
1) TransactionImpl.timeoutExpired(Object arg)
2) Unexpected Exception on commit_one_phase
3) Container cannot commit a transaction

All 3 seem to be related to a synchronize problem of writing data to the
database, hense explains why it works perfectly with 1 client.

I've attached the source code to the insert bean.
The procedure I'm using is as follows:
1) Create Session bean (which opens database connection)
2) Invoke the Insert method (which puts data into the database)
3) Close database


If someone could please look at it and tell me what I'm doing wrong, it would
be greatly appreciated,

Mike


import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;

import java.io.*;


public class InsertBean implements SessionBean
{
    private String dbName = "java:comp/env/jdbc/SupplierDB";
    private	Connection con = null;
    private String name;

    public void Insert(SupplierModel supModel)
	{
 		try
 		{
            insertRow(supModel.getSupplierID(),supModel.getName(),
           	supModel.getTerm(), supModel.getFob(), supModel.getMPO(),
           	supModel.getMPOUnit(), supModel.getMPW(), supModel.getMPWUnit(),
           	supModel.getContact(), supModel.getEmail(), supModel.getTel(),
           	supModel.getFax());
       }
       catch (Exception ex)
       {
           throw new EJBException(ex.getMessage());
       }

  	}
      	
    public void ejbCreate (String str) throws CreateException
    {
    	name=str;
    	try
    	{
    		con = getDBConnection();
    	}
    	catch (Exception e)
    	{
    		System.out.println ("Got connection prob:"+e);
    	}    	
  	}

  	public void ejbRemove()
  	{
		try
		{
			con.close();
    	}
    	catch (Exception e)
    	{
    		System.out.println ("Got connection prob:"+e);
    	}	
    }


   	private Connection getDBConnection() throws NamingException, SQLException
   	{
       	Connection con;
      	try
      	{
	      	InitialContext ic = new InitialContext();
    	  	DataSource ds = (DataSource) ic.lookup(dbName);
			con = ds.getConnection();
		}
		catch (NamingException ne)
		{
			System.out.println("Unable to connect to database.");
			throw new EJBException(ne);
	 	}
	 	catch (SQLException se)
	 	{
			System.out.println("Unable to connect to database.");
	 		throw new EJBException (se);
	 	}
	 	return con;
   	}

    private void insertRow (String supplier_id, String name, String term,
	 		String fob, double mpo, String mpo_unit,
			double mpw, String mpw_unit, String contact,
			String email, String tel, String fax) throws SQLException, IOException
	{
          	
		PreparedStatement prepStmt =  null;
		
	  	try
	  	{	  		
          	String insertStatement =
                "insert into supplier values ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
          	prepStmt = con.prepareStatement(insertStatement);
          	
          	prepStmt.setString(1, supplier_id);
          	prepStmt.setString(2, name);
          	prepStmt.setString(3, term);
          	prepStmt.setString(4, fob);
          	prepStmt.setDouble(5, mpo);
          	prepStmt.setString(6, mpo_unit);
          	prepStmt.setDouble(7, mpw);
          	prepStmt.setString(8, mpw_unit);
          	prepStmt.setString(9, contact);
          	prepStmt.setString(10, email);
          	prepStmt.setString(11, tel);
          	prepStmt.setString(12, fax);
          	prepStmt.executeUpdate();
          	
      	}
      	catch (Exception e)
      	{
	  	}
	  	
	  	finally
	  	{
		 	// Release DB Connection for other beans
			try
			{
				prepStmt.close();
			}
			catch (Exception e)
			{
				System.out.println("------------------------------------------");
				System.out.println("Insert: Can't close the preparedStatement:"+e);
				System.out.println("------------------------------------------");	
		    }
			try
			{
				con.close();
			}
			catch (Exception e)
			{
				System.out.println("------------------------------------------");			
		 		System.out.println("Insert: Can't close the connection to database:"+e);
				System.out.println("------------------------------------------");		 		
 			}
  		}    	
   }
          	   	   	
 	public void ejbActivate()
 	{
    	try
    	{
    		con = getDBConnection();
    	}
    	catch (Exception e)
    	{
    		System.out.println ("Got connection prob:"+e);
    	}    		
 	}
 	
 	public void ejbPassivate()
 	{
 		try
 		{
			con.close(); 	
    	}
    	catch (Exception e)
    	{
    		System.out.println ("Got connection prob:"+e);
    	}

 	}
 	public void setSessionContext(SessionContext sc)
 	{
 	}	
}

Reply via email to