![JAR search and dependency download from the Maven repository](/logo.png)
com.mockrunner.example.ejb.DBStatefulBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Mockrunner is a lightweight framework for unit testing applications
in the J2EE environment. It supports servlets, filters, tag classes
and Struts actions. It includes a JDBC a JMS and a JCA test
framework and can be used to test EJB based applications.
The newest version!
package com.mockrunner.example.ejb;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;
/*
* @ejb:bean name="DBStateful"
* display-name="DBStatefulBean"
* type="Stateful"
* transaction-type="Bean"
* jndi-name="com/mockrunner/example/DBStateful"
**/
/**
* A simple BMT stateful session bean with a method that
* executes an SQL statement.
*/
public class DBStatefulBean implements SessionBean
{
private SessionContext sessionContext;
private UserTransaction transaction;
/*
* @ejb:interface-method
**/
public void beginTransaction()
{
try
{
transaction = sessionContext.getUserTransaction();
transaction.begin();
}
catch(Exception exc)
{
throw new EJBException(exc);
}
}
/*
* @ejb:interface-method
**/
public void executeSQL(String sql)
{
Connection connection = null;
Statement statement = null;
try
{
InitialContext context = new InitialContext();
DataSource dataSource = (DataSource)context.lookup("java:/MySQLDB");
connection = dataSource.getConnection();
statement = connection.createStatement();
statement.execute(sql);
}
catch(Exception exc)
{
throw new EJBException(exc.getMessage());
}
finally
{
try
{
if(null != statement) statement.close();
if(null != connection) connection.close();
}
catch(SQLException sqlExc)
{
}
}
}
/*
* @ejb:interface-method
**/
public void endTransaction(boolean commit)
{
try
{
if(commit)
{
transaction.commit();
}
else
{
transaction.rollback();
}
}
catch(Exception exc)
{
try
{
transaction.rollback();
}
catch(Exception exc2)
{
throw new EJBException(exc2);
}
}
}
/*
* @ejb:create-method
**/
public void ejbCreate() throws CreateException
{
}
public void ejbActivate() throws EJBException, RemoteException
{
}
public void ejbPassivate() throws EJBException, RemoteException
{
}
public void ejbRemove() throws EJBException, RemoteException
{
}
public void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException
{
this.sessionContext = sessionContext;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy