I am new to Hibernate and am trying to put it into my current project which uses JBoss 3.0.6, MySQL 4.0.16, MySQL Java Connector 3.0.9, XDoclet 1.2b3 and Hibernate 2.0.3.
When the create method of the session bean detailed below gets executed, the following exception occurs:
2003-11-25 09:21:15,873 ERROR [net.sf.hibernate.transaction.JDBCTransaction] Begin failed
java.sql.SQLException: You cannot set autocommit during a managed transaction!
at org.jboss.resource.adapter.jdbc.local.LocalManagedConnection.setJdbcAutoCommit(LocalManagedConnection.java:456)
at org.jboss.resource.adapter.jdbc.local.LocalConnection.setAutoCommit(LocalConnection.java:426)
at net.sf.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:40)
at net.sf.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:22)
at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1981)
at cf.com.shinzawaikulgan.ejb.stock.session.ItemSessionBean.create(ItemSessionBean.java:238)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:660)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:77)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:107)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:228)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:92)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:130)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:204)
at org.jboss.ejb.StatelessSessionContainer.invoke(StatelessSessionContainer.java:313)
at org.jboss.ejb.Container.invoke(Container.java:712)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:517)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:382)
at sun.reflect.GeneratedMethodAccessor66.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.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:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:536)
Can someone explain to me what is causing this exception and what I can do to fix it?
I have set up Hibernate to run as a service in JBoss with the following jboss-service.xml:
<server>
<mbean code="net.sf.hibernate.jmx.HibernateService" name="jboss.jca:service=HibernateFactory, name=HibernateFactory">
<depends>jboss.jca:service=RARDeployer</depends>
<depends>jboss.jca:service=LocalTxCM,name=ShinzawaiKulganDS</depends>
<!-- Make it deploy ONLY after DataSource had been started -->
<attribute name="MapResources">cf/com/shinzawaikulgan/hibernate/stock/Item.hbm.xml</attribute>
<attribute name="JndiName">java:/hibernate/shinzawaikulgan/HibernateFactory</attribute>
<attribute name="Datasource">java:/ShinzawaiKulgan</attribute>
<attribute name="Dialect">net.sf.hibernate.dialect.MySQLDialect</attribute>
<attribute name="TransactionStrategy">net.sf.hibernate.transaction.JTATransactionFactory</attribute>
<attribute name="TransactionManagerLookupStrategy">net.sf.hibernate.transaction.JBossTransactionManagerLookup</attribute>
<attribute name="UseOuterJoin">true</attribute>
<attribute name="ShowSql">false</attribute>
</mbean>
</server>
I have a InnoDB table in my database called Item with the matching java file Item.java:
/**
* The class represents an Item
*
* @author $Author:$
* @version $Revision:$
*
* @hibernate.class table="Item"
*
*/
public class Item {
// Constants -----------------------------------------------
// Attributes ----------------------------------------------
private Integer id; // identifier
private String name;
private String description;
private String stockTypeCode;
private String itemCategoryCode;
private String itemQBId;
private Float price;
private char includesTax;
private String supplierId;
private String salesTaxCode;
private char active;
private String addUsername;
private Timestamp addTimestamp;
private String updateUsername;
private Timestamp updateTimestamp;
// Static --------------------------------------------------
// Constructors --------------------------------------------
// Public --------------------------------------------------
/**
* @hibernate.id generator-class="native"
* column="id"
*
* @jboss:column-name name="id"
**/
public Integer getId() {
return id;
}
public void setId( Integer pId ) {
this.id = pId;
}
/**
* @hibernate.property column="name"
* not-null="true"
*
* @jboss:column-name name="name"
**/
public String getName() {
return name;
}
public void setName( String pName ) {
this.name = pName;
}
/**
* @hibernate.property column="description"
* not-null="true"
*
* @jboss:column-name name="description"
**/
public String getDescription() {
return description;
}
public void setDescription( String pDescription ) {
this.description = pDescription;
}
/**
* @hibernate.property column="stockTypeCode"
* not-null="true"
*
* @jboss:column-name name="stockTypeCode"
**/
public String getStockTypeCode() {
return stockTypeCode;
}
public void setStockTypeCode( String pStockTypeCode ) {
this.stockTypeCode = pStockTypeCode;
}
/**
* @hibernate.property column="itemCategoryCode"
*
* @jboss:column-name name="itemCategoryCode"
**/
public String getItemCategoryCode() {
return itemCategoryCode;
}
public void setItemCategoryCode( String pItemCategoryCode ) {
this.itemCategoryCode = pItemCategoryCode;;
}
/**
* @hibernate.property column="itemQBId"
* not-null="true"
*
* @jboss:column-name name="itemQBId"
**/
public String getItemQBId() {
return itemQBId;
}
public void setItemQBId( String pItemQBId ) {
this.itemQBId = pItemQBId;
}
/**
* @hibernate.property column="price"
*
* @jboss:column-name name="price"
**/
public Float getPrice() {
return price;
}
public void setPrice( Float pPrice ) {
this.price = pPrice;
}
/**
* @hibernate.property column="includesTax"
* not-null="true"
*
* @jboss:column-name name="includesTax"
**/
public char getIncludesTax() {
return includesTax;
}
public void setIncludesTax( char pIncludesTax ) {
this.includesTax = pIncludesTax;
}
/**
* @hibernate.property column="supplierId"
*
* @jboss:column-name name="supplierId"
**/
public String getSupplierId() {
return supplierId;
}
public void setSupplierId( String pSupplierId ) {
this.supplierId = pSupplierId;
}
/**
* @hibernate.property column="salesTaxCode"
*
* @jboss:column-name name="salesTaxCode"
**/
public String getSalesTaxCode() {
return salesTaxCode;
}
public void setSalesTaxCode( String pSalesTaxCode ) {
this.salesTaxCode = pSalesTaxCode;
}
/**
* @hibernate.property column="active"
* not-null="true"
*
* @jboss:column-name name="active"
**/
public char getActive() {
return active;
}
public void setActive( char pActive ) {
this.active = pActive;
}
/**
* @hibernate.property column="addUsername"
*
* @jboss:column-name name="addUsername"
**/
public String getAddUsername() {
return addUsername;
}
public void setAddUsername( String pAddUsername ) {
this.addUsername = pAddUsername;
}
/**
* @hibernate.property column="addTimestamp"
*
* @jboss:column-name name="addTimestamp"
**/
public Timestamp getAddTimestamp() {
return addTimestamp;
}
public void setAddTimestamp( Timestamp pAddTimestamp ) {
this.addTimestamp = pAddTimestamp;
}
/**
* @hibernate.property column="updateUsername"
*
* @jboss:column-name name="updateUsername"
**/
public String getUpdateUsername() {
return updateUsername;
}
public void setUpdateUsername( String pUpdateUsername ) {
this.updateUsername = pUpdateUsername;
}
/**
* @hibernate.property column="updateTimestamp"
*
* @jboss:column-name name="updateTimestamp"
**/
public Timestamp getUpdateTimestamp() {
return updateTimestamp;
}
public void setUpdateTimestamp( Timestamp pUpdateTimestamp ) {
this.updateTimestamp = pUpdateTimestamp;
}
The Item class is being used in the following EJB Session Bean:
/**
* The Session bean represents the Item interface
*
* @author $Author:$
* @version $Revision:$
*
* @ejb:bean name="ItemSession"
* display-name="Item Session"
* type="Stateless"
* view-type="remote"
* jndi-name="ejb/shinzawaikulgan/ItemSession"
*
* @ejb:interface extends="javax.ejb.EJBObject"
*
* @ejb:home extends="javax.ejb.EJBHome"
*
* @ejb:pk extends="java.lang.Object"
*
* @ejb:transaction type="None"
*
*/
// * @ejb:ejb-ref ejb-name="Item"
public class ItemSessionBean
extends SessionSupport implements SessionBean
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
private SessionFactory _sessions;
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Private -------------------------------------------------------
private void configureHibernate() throws NamingException /*HibernateException*/ {
Context ctx = new InitialContext();
_sessions = (SessionFactory) ctx.lookup("java:/hibernate/shinzawaikulgan/HibernateFactory");
}
// Public --------------------------------------------------------
/**
* @ejb:interface-method view-type="remote"
**/
public Item create( String pName, String pDescription,
String pStockTypeCode, String pItemCategoryCode, String pItemQBId, Float pPrice,
char pIncludesTax, String pSupplierId, String pSalesTaxCode, char pActive,
String pAddUsername, Timestamp pAddTimestamp )
throws HibernateException, RemoteException, NamingException, SQLException {
System.out.println("Starting Item.create()");
if ( _sessions == null ) configureHibernate();
System.out.println("configured hibernate");
Item item = new Item();
item.setName( pName );
item.setDescription( pDescription );
item.setStockTypeCode( pStockTypeCode );
item.setItemCategoryCode( pItemCategoryCode );
item.setItemQBId( pItemQBId );
item.setPrice( pPrice );
item.setIncludesTax( pIncludesTax );
item.setSupplierId( pSupplierId );
item.setSalesTaxCode( pSalesTaxCode );
item.setActive( pActive );
item.setAddUsername( pAddUsername );
item.setAddTimestamp( pAddTimestamp );
System.out.println("item created");
Session session = _sessions.openSession();
System.out.println("session opened");
Transaction tx = null;
try {
tx = session.beginTransaction();
System.out.println("begin transaction");
session.save(item);
System.out.println("item saved");
tx.commit();
System.out.println("transaction committed");
} catch ( HibernateException he ) {
if ( tx!=null ) tx.rollback();
throw he;
} finally {
session.close();
}
return item;
}
// SessionBean implementation ------------------------------------
public void setSessionContext(SessionContext context)
{
super.setSessionContext(context);
}
}
Any help would be appreciated.
Code:
Code: