0

I am new to JSF/Facelets and I am creating an application that does the usual CRUD operations over a (No SQL) database. The database has an API that allows the creation of a pool of connections, and then from this object my operations can take and release connections. I suppose this pool has to be created only once for the whole application when it is deployed, be shared (as static?) and closed once the application is destroyed. Is my approach correct? What is the best practice to do this? I have no idea of where I should place my code and how I should call it.

With my old SQL database I used to configure a "testOnBorrow" and a "validationQuery" in the context.xml Resource so I didn't have to create an explicit pool programmatically.

I found two great tutorials (here and here) but I can't figure out from them where to put the code that creates the pool.

Note: I know this might be only a Servlet problem, but I am tagging it as JSF since I don't know if there is a way to do this in JSF (like an application scoped bean). Thanks.


EDIT

Looking at the fact that I cannot find a JAR with a DataSource for the database to be loaded via context.xml, perhaps my question should be more specific: where can I run code once, when a JSF application is deployed and where can I run code when a JSF application is destroyed?

user1156544
  • 1,725
  • 2
  • 25
  • 51
  • As long as your connection pool is thread safe, I would go with a `@Singleton` bean. – Aritz Apr 18 '16 at 07:54
  • Are you using standard Java EE stack or a custom Tomcat based stack? Answer depends on that. DAO article assumes custom Tomcat based stack. But you tagged the question [java-ee] which adds a bit ambiguity. Anyway, assuming Tomcat, is this helpful? http://stackoverflow.com/q/2299469 – BalusC Apr 18 '16 at 07:57
  • I think it helps, thanks. I'm using TomEE. So all I need to do is to create a Class that creates the ConnectionPool object, tag it with @Resource("jdbc/some_name"), and then make the link from context.xml " – user1156544 Apr 18 '16 at 08:17
  • The `driverClassName` represents the driver class name of the JDBC driver. It's exactly the value you'd use in `Class#forName()` call in a plain Java application. E.g. `com.mysql.jdbc.Driver` in case you've installed the MySQL JDBC driver. Coming back to the server, given that you're using TomEE, consider moving on to JPA/EJB. It can reduce all the JDBC boilerplate to oneliners. – BalusC Apr 18 '16 at 09:48
  • Thanks for your suggestion. I think there is no JPA implementation for my (NoSQL) database. It also has a lot of JARs so not sure I can find a jdbc driver (I thought JDBC was oriented to relational databases). Now I am totally confused on how to create the pool but I am reading the Tomcat jndi doc to see if I find some light. My first doubt is how to configure the Resource in the context.xml. I know how to do it for MySQL, but not for this database – user1156544 Apr 18 '16 at 10:03
  • Availability of JDBC driver for NoSQL DBs depends on NoSQL DB make/version. Some don't have, some have. At least, it shouldn't be different as compared to standard Java SE application with `main()`. On the other hand, it's kind of contradictory to use SQL to interact with a NoSQL DB. Perhaps you just wanted an embedded SQL DB which can be installed as just a JAR, such as [H2](http://www.h2database.com/html/main.html)? – BalusC Apr 18 '16 at 10:50
  • No, no, I have already access the database and do most code for queries, etc. But the connection takes too long so I need to use a pool to minimise the time. Examples of code are available (ex. https://gist.github.com/mhgrove/1070230), so that's easy, but I don't know where to put the code that creates the pool and how I should call it. I could suppose that what I need is a Custom Resource Factory. And adapt some of this DataSource (https://github.com/Complexible/stardog-spring/tree/master/stardog-spring/src/main/java/com/complexible/stardog/ext/spring) --Note that I don't use Spring – user1156544 Apr 18 '16 at 11:22
  • Servers have builtin connection pooled data sources already. The link in my first comment shows how to utilize it. You just have to specify the JNDI name of the data source in your DAO configuration. – BalusC Apr 18 '16 at 17:03
  • Thx, I read that, and this is how Ive used it with other DBMS (MySQL, Postgre, etc) in the past, but for this one I don't have a JAR to put in the driverClassName, and it's not SQL (and I don't want to use SQL). Because of that I assume TomEE might not be able to create the pool itself (?) and I will have to create it myself when the app starts and destroy it when the app ends (although I have no idea where and how to call it). Unless there is a way to create the "driver" myself, which seems too much work. Im not sure Im understanding the whole picture or if I explain myself properly :/ – user1156544 Apr 19 '16 at 08:10
  • As per your edit, you're basically asking this question? http://stackoverflow.com/q/3468150 – BalusC Apr 21 '16 at 10:44
  • Thanks, looks promising. I will update any findings – user1156544 Apr 21 '16 at 10:47
  • What would it be better practice, using @Observes or using my own Resource Factory in context.xml to deliver connections? – user1156544 Apr 21 '16 at 12:31

1 Answers1

0

You can implement a weblistner( i.e ServletContextListener). and can use contextInitialized , contextDestroyed method of that to create and destroy your connection pool

Sample :

@WebListener
public class ContextWebListner implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // initialize connection pool.
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // Destroy connection pool.
    }

}
thaveethu gce
  • 585
  • 1
  • 9
  • 20