001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.ejb;
023:
024: import java.sql.Connection;
025: import java.sql.SQLException;
026:
027: import javax.ejb.CreateException;
028: import javax.ejb.EJBException;
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import javax.naming.InitialContext;
032: import javax.sql.DataSource;
033:
034: /**
035: * A stateful session bean that has an unshareable resource
036: *
037: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
038: * @version <tt>$Revision: 57211 $</tt>
039: */
040: public class UnshareableConnectionStatefulBean implements SessionBean {
041: /** The serialVersionUID */
042: private static final long serialVersionUID = 1L;
043:
044: Connection c;
045:
046: public void runTestPart1() {
047: try {
048: if (c.getAutoCommit())
049: throw new EJBException("Autocommit should be off");
050: } catch (SQLException e) {
051: throw new EJBException(e.toString());
052: }
053: }
054:
055: public void runTestPart2() {
056: try {
057: c.commit();
058: } catch (SQLException e) {
059: throw new EJBException(e.toString());
060: }
061: }
062:
063: public void ejbCreate() throws CreateException {
064: initConnection();
065: }
066:
067: public void ejbActivate() {
068: initConnection();
069: }
070:
071: public void ejbPassivate() {
072: termConnection();
073: }
074:
075: public void ejbRemove() {
076: termConnection();
077: }
078:
079: public void initConnection() {
080: if (c != null)
081: throw new EJBException("Connection already inited");
082:
083: try {
084: DataSource ds = (DataSource) new InitialContext()
085: .lookup("java:comp/env/jdbc/DataSource");
086: c = ds.getConnection();
087: c.setAutoCommit(false);
088: } catch (Exception e) {
089: throw new EJBException(e.toString());
090: }
091: }
092:
093: public void termConnection() {
094: if (c == null)
095: throw new EJBException("Connection already terminated");
096:
097: try {
098: c.close();
099: c = null;
100: } catch (Exception e) {
101: throw new EJBException(e.toString());
102: }
103: }
104:
105: public void setSessionContext(SessionContext ctx) {
106: }
107:
108: public void unsetSessionContext() {
109: }
110: }
|