john wrote:

> Hi,
>
> I am doing projects with JBoss3 and Castor JDO. I am using Session Bean =
> accessing DB by JDO. There is no problem with using JDBC directly. I =
> know you are using JNDI. Could you share your configuration including =
> database.xml and any other configuration in Jboss3.

Hi John,

Thanks for your reply.

I pasted my database.xml and the generated ejb-jar.xml file below. And
attached you can find the mapping.xml and the actual code of the session
bean and the servlet that use Castor for persistence....
As stated earlier, the update function always throws a RollbackException
with message 'Unable to commit'. I'm completely stuck with this problem.

<database name="test" engine="sql-server">
<jndi name="java:/jdbc/MSSQLDS"/>
<mapping href="/home/admin/eclipse/workspace/J2EEtest1/mapping.xml"/>
</database>


<?xml version="1.0" encoding="UTF-8"?>
<!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 >
<description><![CDATA[No Description.]]></description>
<display-name>Generated by XDoclet</display-name>
<enterprise-beans>
<!-- Session Beans -->
<session >
<description><![CDATA[]]></description>

<ejb-name>DomainBasketLogic</ejb-name>

<home>be.domainnames.J2EEtest1.ejb.DomainBasketLogicHome</home>
<remote>be.domainnames.J2EEtest1.ejb.DomainBasketLogic</remote>
<local-home>be.domainnames.J2EEtest1.ejb.DomainBasketLogicLocalHome</local-home>
<local>be.domainnames.J2EEtest1.ejb.DomainBasketLogicLocal</local>
<ejb-class>be.domainnames.J2EEtest1.ejb.DomainBasketLogicSession</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>

</session>

<session >
<description><![CDATA[]]></description>

<ejb-name>ContactService</ejb-name>

<home>be.domainnames.J2EEtest1.ejb.ContactServiceHome</home>
<remote>be.domainnames.J2EEtest1.ejb.ContactService</remote>
<local-home>be.domainnames.J2EEtest1.ejb.ContactServiceLocalHome</local-home>
<local>be.domainnames.J2EEtest1.ejb.ContactServiceLocal</local>
<ejb-class>be.domainnames.J2EEtest1.ejb.ContactServiceSession</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>

<resource-ref >
<res-ref-name>jdo/CastorJDO</res-ref-name>
<res-type>org.exolab.castor.jdo.DataObjects</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</session>
</enterprise-beans>
<assembly-descriptor >
<container-transaction >
<method >
<ejb-name>ContactService</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction >
<method >
<ejb-name>ContactService</ejb-name>
<method-intf>Local</method-intf>
<method-name>getContact</method-name>
<method-params>
<method-param>long</method-param>
</method-params>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction >
<method >
<ejb-name>ContactService</ejb-name>
<method-intf>Remote</method-intf>
<method-name>getContact</method-name>
<method-params>
<method-param>long</method-param>
</method-params>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction >
<method >
<ejb-name>ContactService</ejb-name>
<method-intf>Local</method-intf>
<method-name>updateContact</method-name>
<method-params>
<method-param>be.domainnames.J2EEtest1.castor.Contact</method-param>
</method-params>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
<container-transaction >
<method >
<ejb-name>ContactService</ejb-name>
<method-intf>Remote</method-intf>
<method-name>updateContact</method-name>
<method-params>
<method-param>be.domainnames.J2EEtest1.castor.Contact</method-param>
</method-params>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

</ejb-jar>

<?xml version="1.0" encoding="UTF-8"?>
<mapping xmlns="http://castor.exolab.org/";>
<!-- CONTACTS -->
<class name="be.domainnames.J2EEtest1.castor.Contact" identity="contactId" access="shared" key-generator="IDENTITY">
	<cache-type type="count-limited" capacity="5000" />
	<map-to table="contacts"/>
	<field name="contactId" type="long">
		<sql name="contactId" type="integer" dirty="check" read-only="false"/>
	</field>
	<field name="firstName" type="string">
		<sql name="firstName" type="varchar" dirty="check" read-only="false"/>
	</field>
	<field name="lastName" type="string">
		<sql name="lastName" type="varchar" dirty="check" read-only="false"/>
	</field>
</class>
</mapping>
package be.domainnames.J2EEtest1.ejb;

import javax.ejb.SessionBean;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;

import org.exolab.castor.jdo.DataObjects;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.OQLQuery;
import org.exolab.castor.jdo.QueryResults;
import org.exolab.castor.jdo.DataObjects;
import org.exolab.castor.jdo.PersistenceException;

import be.domainnames.J2EEtest1.castor.*;

/**
 * @author [EMAIL PROTECTED]
 * Business Interfacing BVBA, Leuven - (c) 2003
 *
 * @ejb.bean			type="Stateless"
 * 						name="ContactService"
 * 						jndi-name="ejb/ContactService"
 * @ejb.transaction
 *						type="Required"
 * 
 * @ejb:resource-ref 	res-name="jdo/CastorJDO"
 *                   	res-type="org.exolab.castor.jdo.DataObjects"
 *                   	res-auth="Container"
 *
 * @jboss:resource-ref  res-ref-name="jdo/CastorJDO"
 *                      resource-name="CastorJDO"
 *
 * @jboss:resource-manager res-man-class="org.jboss.ejb.deployment.CastorJDOResource"
 *                         res-man-name="CastorJDO"
 *                         res-man-jndi-name="java:/jdo/CastorJDO"
 *
 * @jboss:resource-manager res-man-class="org.jboss.ejb.deployment.JDBCResource"
 *                         res-man-name="jdbc/MSSQLDS"
 *                         res-man-jndi-name="java:/jdbc/MSSQLDS"
**/


public abstract class ContactSession implements SessionBean {

	/**
	 * @ejb.interface-method
	 *	view-type="both"
	*
	**/
	public String helloworld() throws ServerException {
		return "gooooooooooood.";
	}

	/**
	* @ejb.interface-method
	*		view-type="both"
	* @ejb.transaction
	*		type="Required"
	*
	**/
	public Contact getContact(long contactId) throws ServerException{
		Contact contact = null;
		DataObjects _jdo;
        Database      db = null;
        OQLQuery      findOql;
        QueryResults  results;
        Object data = null;
        try {
	        InitialContext ic = new InitialContext();
			_jdo = (DataObjects) ic.lookup("java:/jdo/CastorJDO");
            db = _jdo.getDatabase();
            findOql = db.getOQLQuery("Select c from be.domainnames.J2EEtest1.castor.Contact c where contactId = 81");
            results=findOql.execute();
            if ( results.hasMore() ) {
                contact = (Contact)results.next();
            }
        } catch(Exception e){
            System.err.println(e.getMessage());
            e.printStackTrace();
            int cause = ServerException.UNKNOWN;
            if (e instanceof java.lang.IllegalStateException)
              cause = ServerException.DODGY_MODEL;
            throw new ServerException("Unable to extract entity from database: "+e.getMessage(),cause);
        }
        finally {
            try{
                if(db!=null) db.close();
            } catch(PersistenceException e) {
                System.err.println("Could not close a database connection:"+e.getMessage());
                e.printStackTrace(System.err);
            }
        }
		return contact;
//		CastorHelper castor = new CastorHelper();
//		return (Contact)castor.findSingleton("select c from be.domainnames.J2EEtest1.castor.Contact c where c.contactId = " + contactId);
	}
	/**
	* @ejb.interface-method
	*		view-type="both"
	* @ejb.transaction
	*		type="Required"
	*
	**/
	public void updateContact(Contact contact) throws ServerException{
		DataObjects _jdo;
        Database      db = null;
        OQLQuery      findOql;
        QueryResults  results;
        Object data = null;
        try {
	        InitialContext ic = new InitialContext();
			_jdo = (DataObjects) ic.lookup("java:/jdo/CastorJDO");
            db = _jdo.getDatabase();
            //db.begin();
            //UserTransaction userTransaction = (UserTransaction) ic.lookup("UserTransaction");
            //userTransaction.begin();
            db.update(contact);
            //userTransaction.commit();
            //db.commit();
        } catch(Exception e){
        	System.out.println("ERROR UPDATING");
        	e.printStackTrace();
//            int cause = ServerException.UNKNOWN;
//            if (e instanceof java.lang.IllegalStateException)
//              cause = ServerException.DODGY_MODEL;
 //           throw new ServerException("Unable to extract entity from database: "+e.getMessage(),cause);
        }
        finally {
            try{
                if(db!=null) db.close();
            } catch(PersistenceException e) {
                System.err.println("Could not close a database connection:"+e.getMessage());
            }
        }
		//return (Contact)castor.findSingleton("select c from be.domainnames.J2EEtest1.castor.Contact c where c.contactId = " + contactId);
	}
	
	public void ejbActivate() {
		System.out.println("ContactSession::activate");
	}

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

	public void setSessionContext(javax.ejb.SessionContext ctx) {
		System.out.println("ContactSession::setSessionContext");
		
	}

	public void unsetSessionContext() {
		System.out.println("ContactSession::unsetSessionContext");
	}

	public void ejbRemove() {
		System.out.println("ContactSession::remove");
	}

	public void ejbCreate() throws javax.ejb.CreateException {
		System.out.println("ContactSession::create");
		
	}

}
package be.domainnames.J2EEtest1.servlets;

import java.io.IOException;

import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;

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

import be.domainnames.J2EEtest1.castor.*;
import be.domainnames.J2EEtest1.ejb.*;

/**
 * 
 * @web.servlet
 *  display-name = "Test Servlet"
 *  name = "TestServlet"
 * 
 * @web.servlet-mapping
 *  url-pattern = "/TestServlet"
 * 
 * 
 * 
 * @author [EMAIL PROTECTED]
 *
 * Business Interfacing BVBA, Leuven - (c) 2003
 */
public class TestServlet extends HttpServlet {
	
	

	/**
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		try{
			response.getWriter().println("It works - J2EEtest1 project");		
			InitialContext ctx = new InitialContext();
			Object ref = ctx.lookup("ejb/ContactService");
			ContactServiceHome contactServiceHome = (ContactServiceHome)ref;
			ContactService contactService = (ContactService)contactServiceHome.create();
			try{		
	//			userTransaction.begin();
				if (request.getParameter("execute")==null){
					System.out.println("reading...........");
					Contact contact = null;
					contact = contactService.getContact(81);
					response.getWriter().println("hello from " + contact.getFirstName() + " " + contact.getLastName() + " (" + contact.getCountry().getCountryCode() + ")");
					contact = contactService.getContact(82);
					response.getWriter().println("hello from " + contact.getFirstName() + " " + contact.getLastName());
					contact = contactService.getContact(83);
					response.getWriter().println("hello from " + contact.getFirstName() + " " + contact.getLastName());
					contact = contactService.getContact(84);
					response.getWriter().println("hello from " + contact.getFirstName() + " " + contact.getLastName());
				} else {
					System.out.println("updating...........");
					Contact contact = null;
					contact = contactService.getContact(81);
					contact.setFirstName("jan");
					contactService.updateContact(contact);
					response.getWriter().print("updated data of " + contact.getFirstName() + " " + contact.getLastName());
				}		
	//			userTransaction.commit();
			} catch(Exception e){
	//			userTransaction.rollback();
				e.printStackTrace();
				throw new ServerException(e.getMessage());
			}
		}catch(Exception e){
			System.out.println("***ERROR***" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest, HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		super.doGet(request, response);
	}

}

Reply via email to