Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] WebSocketHandler or WebSocketListener - where to open JDBC connection

Use a custom WebSocketCreator.

See the examples here -> http://stackoverflow.com/a/15649791/775715

Joakim Erdfelt / joakim@xxxxxxxxxxx

On Sun, Jun 5, 2016 at 1:02 AM, Alexander Farber <alexander.farber@xxxxxxxxx> wrote:
More to my problem -

if I move database opening code into the custom WebsocketListener implementation https://github.com/afarber/jetty-newbie/blob/master/WebsocketHandler/src/main/java/de/afarber/MyListener.java

   @Override
    public void onWebSocketConnect(Session session) {
        Properties props = new Properties();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        try (
                InputStream is = loader.getResourceAsStream("database.properties");
        ) {
            props.load(is);
        } catch (IOException ex) {
            return;
        }
        
        try {
            mDatabase = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/", props);
        } catch (SQLException ex) {
            return;
        }
 
        mName = "Client-" + ThreadLocalRandom.current().nextInt(1, 1000 + 1);
        mSession = session;
        sClients.put(mName, mSession);
        messageAll(mName + " has connected to " + getClass().getName());
    }

then the code of reading database.properties is run again and again.

I wonder if it could be run once somewhere in WebSocketServletFactory instead - and then passed to the registered class.

Would creating a custom WebsocketCreator help here?

Regards
Alex

On Sun, Jun 5, 2016 at 8:54 AM, Alexander Farber <alexander.farber@xxxxxxxxx> wrote:
But actually I already have a "pool manager" - in the form of pgbouncer in front of my PostgreSQL server


_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users


Back to the top