KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > ejb > UnshareableConnectionStatefulBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.ejb;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 import javax.ejb.CreateException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.SessionBean JavaDoc;
30 import javax.ejb.SessionContext JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33
34 /**
35  * A stateful session bean that has an unshareable resource
36  *
37  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
38  * @version <tt>$Revision: 37406 $</tt>
39  */

40 public class UnshareableConnectionStatefulBean
41    implements SessionBean JavaDoc
42 {
43    /** The serialVersionUID */
44    private static final long serialVersionUID = 1L;
45
46    Connection JavaDoc c;
47
48    public void runTestPart1()
49    {
50       try
51       {
52          if (c.getAutoCommit())
53             throw new EJBException JavaDoc("Autocommit should be off");
54       }
55       catch (SQLException JavaDoc e)
56       {
57          throw new EJBException JavaDoc(e.toString());
58       }
59    }
60
61    public void runTestPart2()
62    {
63       try
64       {
65          c.commit();
66       }
67       catch (SQLException JavaDoc e)
68       {
69          throw new EJBException JavaDoc(e.toString());
70       }
71    }
72
73    public void ejbCreate()
74       throws CreateException JavaDoc
75    {
76       initConnection();
77    }
78
79    public void ejbActivate()
80    {
81       initConnection();
82    }
83
84    public void ejbPassivate()
85    {
86       termConnection();
87    }
88
89    public void ejbRemove()
90    {
91       termConnection();
92    }
93
94    public void initConnection()
95    {
96       if (c != null)
97          throw new EJBException JavaDoc("Connection already inited");
98
99       try
100       {
101          DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env/jdbc/DataSource");
102          c = ds.getConnection();
103          c.setAutoCommit(false);
104       }
105       catch (Exception JavaDoc e)
106       {
107          throw new EJBException JavaDoc(e.toString());
108       }
109    }
110
111    public void termConnection()
112    {
113       if (c == null)
114          throw new EJBException JavaDoc("Connection already terminated");
115
116       try
117       {
118          c.close();
119          c = null;
120       }
121       catch (Exception JavaDoc e)
122       {
123          throw new EJBException JavaDoc(e.toString());
124       }
125    }
126
127    public void setSessionContext(SessionContext JavaDoc ctx)
128    {
129    }
130
131    public void unsetSessionContext()
132    {
133    }
134 }
135
136
Popular Tags