Spark: Connection pooling in Spark Java framework

Created on 16 Apr 2017  路  4Comments  路  Source: perwendel/spark

Java Spark framework uses embedded Jetty as as web server. Jetty supports connection pooling with tools such as HikariCP and provides configuration options in XML files. However, according to these posts, Spark does allow to configure Jetty. There are plenty of examples using Spark, but they either do not use database or use DriverManager to connect to the database.

Is it possible to configure connection pooling via datasource and JNDI in Spark? If so, how?

It would be helpful for everyone if connection pools can be configured in sparkjava using jetty.

Most helpful comment

There is a question on this Stackoverflow. If you can answer then that would be really helpful. Thank you

All 4 comments

If you scroll to the end of the issue you linked to, you'll see that #813 has been merged, which should make Jetty fully configurable.

Does #813 allows configuring connection pools? If I am not wrong, #813 allows only configuring threads

There is a question on this Stackoverflow. If you can answer then that would be really helpful. Thank you

I also would appreciate an example of jetty + Postgresql connection pooling example/snippets.
It is really unclear how to do this from within Spark, when jetty-web.xml and web.xml cannot be used.

I have figured out how to expose the 'jetty' server, but not clear on how I I would get it to do connection pooling

/* this should be in main before any other sparkjava to jetty directives */


        EmbeddedServers.add(EmbeddedServers.Identifiers.JETTY, (Routes routeMatcher,
                                                                StaticFilesConfiguration staticFilesConfiguration,
                                                                boolean hasMultipleHandler) ->
        {
            JettyHandler handler = setupHandler(routeMatcher, staticFilesConfiguration, hasMultipleHandler);

            //handler.getSessionCookieConfig().setName("XSESSION");


            //copied implementation of the factory class from
            //https://github.com/perwendel/spark/blob/master/src/main/java/spark/embeddedserver/jetty/JettyServer.java
            //exposing the code for factory implementation, and above -- for handler
            //essentially allows me to control all Jetty settings (which is what I need for various tunings...)
            JettyServerFactory mf=new JettyServerFactory() {
                @Override
                public Server create(int maxThreads, int minThreads, int threadTimeoutMillis) {
                    Server server;

                    if (maxThreads > 0) {
                        int max = maxThreads;
                        int min = (minThreads > 0) ? minThreads : 8;
                        int idleTimeout = (threadTimeoutMillis > 0) ? threadTimeoutMillis : 60000;

                        server = new Server(new QueuedThreadPool(max, min, idleTimeout));
                    } else {
                        server = new Server();
                    }

                    /* HERE I can set any Jetty parmeters I want... 
however, all examples of Jetty connection pooling for postgres are 
configured within jetty-web.xml, and I do not have a way to that

Therefore this question.
 */
                    return server;
                }

                @Override
                public Server create(ThreadPool threadPool) {
                    return threadPool != null ? new Server(threadPool) : new Server();
                }
            };
            EmbeddedJettyServer mj=new EmbeddedJettyServer(mf,handler);

            return mj;


        });

 /**
     * setup handler in the same manner spark does in {@code EmbeddedJettyFactory.create()}.
     *
     * @see <a href="https://github.com/perwendel/spark/blob/master/src/main/java/spark/embeddedserver/jetty/EmbeddedJettyFactory.java#L39">EmbeddedJettyFactory.java</a>
     */
    private static JettyHandler setupHandler(Routes routeMatcher, StaticFilesConfiguration staticFilesConfiguration, boolean hasMultipleHandler) {
        MatcherFilter matcherFilter = new MatcherFilter(routeMatcher, staticFilesConfiguration, false, hasMultipleHandler);
        matcherFilter.init(null);

        return new JettyHandler(matcherFilter);
    }
Was this page helpful?
0 / 5 - 0 ratings