KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > session > SequenceGeneratorBean


1 package hero.session;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.Statement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import javax.ejb.CreateException JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.ejb.SessionBean JavaDoc;
12 import javax.ejb.SessionContext JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.naming.ServiceUnavailableException JavaDoc;
17 import javax.sql.DataSource JavaDoc;
18
19 /**
20  * Encapsulates the retrival of DB data
21  *
22  * @author Andreas Schaefer
23  * @version $Revision: 1.2 $
24  *
25  * @ejb:bean name="SequenceGenerator"
26  * display-name="Generates unique Identifier for an Entity"
27  * type="Stateless"
28  * jndi-name="ejb/hero/SequenceGenerator"
29  * @ejb.permission role-name="BONITAUSER,SuperAdmin"
30  *
31  * @ejb:transaction type="Supports"
32  *
33  * @jonas.bean
34  * ejb-name="SequenceGenerator"
35  * jndi-name="ejb/hero/SequenceGenerator"
36  *
37  *
38  * @ejb.resource-ref res-ref-name="jdbc/myDS"
39  * res-type="javax.sql.DataSource"
40  * res-auth="Application"
41  * @jonas.resource
42  * res-ref-name="jdbc/myDS"
43  * jndi-name="bonita"
44  *
45  *
46  *
47 */

48 public class SequenceGeneratorBean
49    implements SessionBean JavaDoc
50 {
51
52    // -------------------------------------------------------------------------
53
// Static
54
// -------------------------------------------------------------------------
55

56    // -------------------------------------------------------------------------
57
// Members
58
// -------------------------------------------------------------------------
59

60    private SessionContext JavaDoc mContext;
61    // Only for test purposes
62
private int mNextNumber = 0;
63
64    // -------------------------------------------------------------------------
65
// Methods
66
// -------------------------------------------------------------------------
67

68    /**
69     * Delivers the next sequence number from the given Sequence Name
70     *
71     * @param pSequenceName Name of the Sequence Generator
72     *
73     * @return Next sequence number
74     *
75     * @throws RemoteException
76     *
77     * @ejb:interface-method view-type="remote"
78     * @ejb:transaction type="Supports"
79     **/

80    public int getNextNumber( String JavaDoc pSequenceName )
81       throws
82          ServiceUnavailableException JavaDoc,
83          RemoteException JavaDoc
84    {
85       Connection JavaDoc aConnection = null;
86       Statement JavaDoc aStatement = null;
87       try
88       {
89 /* This normally works for DBs like PostgreSQL, Oracle and others
90          // Get the Home Interface
91          Context aJNDIContext = new InitialContext();
92          String lDataSourceName = (String) aJNDIContext.lookup(
93             "java:comp/env/DataSource_Name"
94          );
95          // Get the Datasource
96          DataSource aSource = (DataSource) aJNDIContext.lookup( "java:/" + lDataSourceName );
97          // Get JDBC Connection, create statement and get the result to return
98          aConnection = aSource.getConnection();
99          aStatement = aConnection.createStatement();
100          String aSql = "SELECT Nextval( '" + pSequenceName + "' ) ";
101          if( Debug.LEVEL >= Debug.REGULAR ) System.err.println( "Sql Statement: " + aSql );
102          ResultSet aResult = aStatement.executeQuery( aSql );
103          if( aResult.next() )
104          {
105             return aResult.getInt( 1 );
106          }
107          else
108          {
109             return -1;
110          }
111 */

112 // Hypersonic does not provide a feature like Sequences therefore
113
// we just use the highest ID and add 1 to it. Because this method
114
// requireds a transaction this method will block any call from other
115
// Beans until the transaction is finished.
116
// ATTENTION: this works fine as long as all EJBs get their new unique
117
// ID from there and nobody adds a new record to the DB other than through
118
// this application server.
119
// Get the Home Interface
120
Context JavaDoc aJNDIContext = new InitialContext JavaDoc();
121         DataSource JavaDoc aSource = (DataSource JavaDoc) aJNDIContext.lookup("java:comp/env/jdbc/myDS");
122         aConnection = aSource.getConnection();
123         aStatement = aConnection.createStatement();
124         String JavaDoc aSql = "SELECT COUNT( * ) FROM "+pSequenceName;
125         ResultSet JavaDoc aResult = aStatement.executeQuery( aSql );
126         int lResult = -1;
127         if( aResult.next() ) {
128         lResult = aResult.getInt( 1 );
129         if( lResult <= 0 ) {
130         lResult = 0;
131         }
132         }
133         return lResult + 1;
134
135          
136          
137       }
138       catch( SQLException JavaDoc se ) {se.printStackTrace();
139          throw new ServiceUnavailableException JavaDoc ( "Sequence number is broken" );
140       }
141       catch( NamingException JavaDoc ne ) {
142          throw new ServiceUnavailableException JavaDoc ( "JNDI Lookup broken" );
143       }
144       finally {
145          try {
146             if( aStatement != null ) {
147                aStatement.close();
148             }
149          }
150          catch( Exception JavaDoc e ) {
151          }
152          try {
153             if( aConnection != null ) {
154                aConnection.close();
155             }
156          }
157          catch( Exception JavaDoc e ) {
158          }
159       }
160    }
161
162    /**
163     * Create the Session Bean
164     *
165     * @throws CreateException
166     *
167     * @ejb:create-method view-type="remote"
168     **/

169    public void ejbCreate()
170       throws
171          CreateException JavaDoc
172    {
173    }
174
175    /**
176     * Describes the instance and its content for debugging purpose
177     *
178     * @return Debugging information about the instance and its content
179     * @ejb:transaction type="Supports"
180     **/

181    public String JavaDoc toString()
182    {
183       return "SequenceGeneratorBean [ " + " ]";
184    }
185
186    // -------------------------------------------------------------------------
187
// Framework Callbacks
188
// -------------------------------------------------------------------------
189

190    public void setSessionContext( SessionContext JavaDoc aContext )
191       throws EJBException JavaDoc
192    {
193       mContext = aContext;
194    }
195    
196    public void ejbActivate()
197       throws EJBException JavaDoc
198    {
199    }
200    
201    public void ejbPassivate()
202       throws EJBException JavaDoc
203    {
204    }
205
206    public void ejbRemove()
207       throws EJBException JavaDoc
208    {
209    }
210
211 }
212
Popular Tags