hi,
I am trying to develop a BMP using MS access as database and
Jboss. My bean first makes a connection to the database, and then it inserts a row, updates, finds by primary key etc.
I have some doubts -
1. There is a file called msaccess-service.xml under [JBoss Home]/examples/jca. In this i modified the dsn name and also specified the DB name as per my program. My doubt is, should msaccess-service.xml be copied and pasted under conf directory?
2. Should there be a jaws.xml file in the
ejb jar? If so, what should be the format of jaws.xml.
Thanks in advance,
Dee Nara.
I get the following error when i run the client:
16:45:50,843 INFO [STDOUT] Got connection
16:45:50,873 INFO [STDOUT] make connection works
16:45:50,883 ERROR [LogInterceptor] EJBException:
javax.ejb.EJBException: ejbCreate():dbInsertRowCalled with isbn: 078063344X titl
e: Best of Show format: WS exception: null
at DvdBean.ejbCreate(DvdBean.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.plugins.BMPPersistenceManager.createEntity(BMPPersisten
ceManager.java:220)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.crea
teEntity(CachedConnectionInterceptor.java:270)
at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:731)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityC
ontainer.java:1119)
at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invokeHome(Ent
itySynchronizationInterceptor.java:206)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invo
keHome(CachedConnectionInterceptor.java:215)
at org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractIntercep
tor.java:73)
at org.jboss.ejb.plugins.EntityInstanceInterceptor.invokeHome(EntityInst
anceInterceptor.java:90)
at org.jboss.ejb.plugins.EntityLockInterceptor.invokeHome(EntityLockInte
rceptor.java:79)
at org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCrea
tionInterceptor.java:44)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInte
rceptor.java:111)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxIntercep
torCMT.java:178)
at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.ja
va:52)
at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityIntercep
tor.java:105)
at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:1
29)
at org.jboss.ejb.EntityContainer.invokeHome(EntityContainer.java:487)
at org.jboss.ejb.Container.invoke(Container.java:730)
at org.jboss.ejb.EntityContainer.invoke(EntityContainer.java:1058)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:517)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:
382)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:701)
at java.lang.Thread.run(Thread.java:536)
My Bean code :
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
public class DvdBean implements EntityBean {
private EntityContext entityContext;
private Connection connection;
private
String dbName = "java:comp/env/dvdejb/jdbc/DvdDB";
/*bean state fields*/
private String isbn; //the primary key
private String title;
private String format;
/*methods in remote interface, access state fields*/
public void setTitle(String newTitle)
{
this.title = newTitle;
}
public void setFormat(String newFormat)
{
this.format = newFormat;
}
public String getTitle()
{
return title;
}
public String getFormat()
{
return format;
}
public String getAsString()
{
return("dvd = isbn: " + isbn + " title: " + title + " format: " + format);
}
/*methods in home interface, use table(s) */
public String ejbCreate(String isbn, String title, String format) throws CreateException
{
try
{
dbInsertRow(isbn, title, format);
} catch (Exception exception) {throw new EJBException("ejbCreate():dbInsertRowCalled with isbn: " + isbn + " title: " + title + " format: " + format + " exception: " + exception.getMessage());}
this.isbn = isbn;
this.title = title;
this.format = format;
return isbn;
}
public String ejbFindByPrimaryKey(String primaryKey) throws FinderException
{
boolean result;
try
{
result = dbSelectByPrimaryKey(primaryKey);
} catch (Exception exception) {throw new EJBException("ejbFindByPrimaryKey(): after calling dbSelectByPrimaryKey " + exception.getMessage());}
if (result)
{
return primaryKey;
}
else
{
throw new ObjectNotFoundException ("ejbFindByPrimaryKey(): Row for id " + primaryKey + " not found.");
}
}
//differs from ejbFindByPrimaryKey in that it returns a collection, and so can return a null set
public Collection ejbFindByIsbn(String primaryKey) throws FinderException
{
Collection result;
try
{
result = dbSelectByIsbn(primaryKey);
} catch (Exception exception) {throw new EJBException("ejbCheckForPrimaryKey(): after calling dbSelectByIsbn " + exception.getMessage());}
return result;
}
public Collection ejbFindByTitle(String title) throws FinderException
{
Collection result;
try
{
result = dbSelectByTitle(title);
} catch (Exception exception) {throw new EJBException("ejbFindByTitle: after calling dbSelectByTitle() " + exception.getMessage());}
return result;
}
public Collection ejbFindByTitleWith(String title) throws FinderException
{
Collection result;
try
{
result = dbSelectByTitleWith(title);
} catch (Exception exception) {throw new EJBException("ejbFindByTitleWith: after calling dbSelectTitleWith() " + exception.getMessage());}
return result;
}
/*methods necessary in an entity bean */
public void ejbRemove()
{
try
{
dbDeleteRow(isbn);
System.out.println("delete row works");
} catch (Exception exception) {throw new EJBException("ejbRemove(): after calling dbDeleteRow() " + exception.getMessage());}
}
public void setEntityContext(EntityContext entityContext)
{
this.entityContext = entityContext;
try {
dbMakeConnection();
System.out.println("make connection works");
} catch (Exception exception) {throw new EJBException("setEntityContext(): after calling dbMakeConnection() " + exception.getMessage());}
}
public void unsetEntityContext()
{
try {
connection.close();
System.out.println("connection close works");
} catch (SQLException exception) {throw new EJBException("unsetEntityContext(): closing connection" + exception.getMessage());}
}
public void ejbActivate()
{
isbn = (String)entityContext.getPrimaryKey();
System.out.println("primary key works");
}
public void ejbPassivate() {
isbn = null;
}
public void ejbLoad()
{
try {
dbLoadRow();
System.out.println("ejbload works");
} catch (Exception exception) {throw new EJBException("ejbLoad():dbLoadRow called using this.isbn: " + this.isbn + " exception " + exception.getMessage());}
}
public void ejbStore()
{
try {
dbStoreRow();
System.out.println("ejb store works");
} catch (Exception exception) {throw new EJBException("ejbStore(): " + exception.getMessage());
}
}
public void ejbPostCreate(String isbn, String title, String format) { }
/*db methods*/
private void dbMakeConnection() throws NamingException, SQLException
{
InitialContext initialContext = new InitialContext();
System.out.println("Got connection");/** for debug*/
DataSource dataSource = (DataSource)initialContext.lookup(dbName);
Connection connection = dataSource.getConnection();
}
private void dbInsertRow (String isbn, String title, String format) throws SQLException
{
String insertStatement = "insert into dvd values ( ? , ? , ? )";
PreparedStatement preparedStatement = connection.prepareStatement(insertStatement);
preparedStatement.setString(1, isbn);
preparedStatement.setString(2, title);
preparedStatement.setString(3, format);
preparedStatement.executeUpdate();
preparedStatement.close();
}
private void dbDeleteRow(String isbn) throws SQLException
{
String deleteStatement = "delete from dvd where isbn = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(deleteStatement);
preparedStatement.setString(1, isbn);
preparedStatement.executeUpdate();
preparedStatement.close();
}
private boolean dbSelectByPrimaryKey(String primaryKey) throws SQLException
{
String selectStatement = "select isbn from dvd where isbn = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(selectStatement);
preparedStatement.setString(1, primaryKey);
ResultSet resultSet = preparedStatement.executeQuery();
boolean result = resultSet.next();
preparedStatement.close();
return result;
}
private Collection dbSelectByIsbn(String isbnIn) throws SQLException
{
String selectStatement = "select isbn from dvd where isbn = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(selectStatement);
preparedStatement.setString(1, isbnIn);
ResultSet resultSet = preparedStatement.executeQuery();
ArrayList arrayList = new ArrayList();
while (resultSet.next()) {
String isbn = resultSet.getString(1);
arrayList.add(isbn);
}
preparedStatement.close();
return arrayList;
}
private Collection dbSelectByTitle(String title) throws SQLException
{
String selectStatement = "select isbn from dvd where title = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(selectStatement);
preparedStatement.setString(1, title);
ResultSet resultSet = preparedStatement.executeQuery();
ArrayList arrayList = new ArrayList();
while (resultSet.next()) {
String isbn = resultSet.getString(1);
arrayList.add(isbn);
}
preparedStatement.close();
return arrayList;
}
private Collection dbSelectByTitleWith(String title) throws SQLException
{
String selectStatement = "select isbn from dvd where title like ? ";
PreparedStatement preparedStatement = connection.prepareStatement(selectStatement);
preparedStatement.setString(1, "%" + title + "%");
ResultSet resultSet = preparedStatement.executeQuery();
ArrayList arrayList = new ArrayList();
while (resultSet.next()) {
String isbn = resultSet.getString("isbn");
arrayList.add(isbn);
}
preparedStatement.close();
return arrayList;
}
private void dbLoadRow() throws SQLException
{
String selectStatement = "select title, format from dvd where isbn = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(selectStatement);
preparedStatement.setString(1, this.isbn);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
this.title = resultSet.getString(1);
this.format = resultSet.getString(2);
preparedStatement.close();
}
else {
preparedStatement.close();
throw new NoSuchEntityException("dbLoadRow(): Row for isbn " + isbn + " not found in database.");
}
}
private void dbStoreRow() throws SQLException
{
String updateStatement = "update dvd set title = ? , format = ? where isbn = ?";
PreparedStatement preparedStatement = connection.prepareStatement(updateStatement);
preparedStatement.setString(1, title);
preparedStatement.setString(2, format);
preparedStatement.setString(3, isbn);
int rowCount = preparedStatement.executeUpdate();
preparedStatement.close();
if (rowCount == 0) {
throw new EJBException("dbStoreRow(): Storing row for isbn " + isbn + " failed.");
}
}
}