Hi Ralph, A session is intended to be a long-lived, i.e., application-scoped object. You only need one session per cluster. I think what you are doing with the @Singleton is fine. In my opinion though, EJB really does not offer much value when working with Cassandra. I would be inclined to just use CDI.
Cheers John On Tue, Jun 11, 2019 at 5:38 PM Ralph Soika <ralph.so...@imixs.com> wrote: > Hi, > > I have a question concerning the Cassandra DataStax Java Driver in > combination with Java EE and EJBs. > > I have implemented a Rest Service API based on Java EE8. In my application > I have for example a jax-rs rest resource to write data into cassandra > cluster. My first approach was to create in each method call > > 1. a new Casssandra Cluster and Session object, > 2. write my data into cassandra > 3. and finally close the session and the cluster object. > > This works but it takes a lot of time (2-3 seconds) until the cluster > object / session is opened for each request. > > So my second approach is now a @Singleton EJB providing the session > object for my jax-rs resources. My service implementation to hold the > Session object looks something like this: > > > *@Singleton* > *public class* ClusterService { > private Cluster cluster; > private Session session; > > @PostConstruct > *private void* init() throws ArchiveException { > cluster=initCluster(); > session = initArchiveSession(); > } > > @PreDestroy > *private* void tearDown() throws ArchiveException { > // close session and cluster object > if (session != null) { > session.close(); > } > if (cluster != null) { > cluster.close(); > } > } > > *public* Session getSession() { > if (session==null) { > try { > init(); > } catch (ArchiveException e) { > logger.warning("unable to get falid session: " + > e.getMessage()); > e.printStackTrace(); > } > } > *return* session; > } > > ..... > > } > > > And my rest service calls now looking like this: > > > @Path("/archive") > @Stateless > *public class* ArchiveRestService { > > @EJB > ClusterService clusterService; > > @POST > @Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML }) > *public* Response postData(XMLDocument xmlDocument) { > Session session = clusterService.getSession(); > session.execute(....); > ... > } > ... > } > > > The result is now a super-fast behavior! Seems to be clear because my > rest service no longer need to open a new session for each request. > > My question is: Is this approach with a @Singleton ClusterService EJB > valid or is there something I should avoid? > As far as I can see this works pretty fine and is really fast. I am > running the application on a Wildfly 15 server which is Java EE8. > > Thanks for your comments > > Ralph > > > > > -- > > *Imixs Software Solutions GmbH* > *Web:* www.imixs.com *Phone:* +49 (0)89-452136 16 > *Office:* Agnes-Pockels-Bogen 1, 80992 München > Registergericht: Amtsgericht Muenchen, HRB 136045 > Geschaeftsführer: Gaby Heinle u. Ralph Soika > > *Imixs* is an open source company, read more: www.imixs.org > -- - John