Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iKUBH-00023s-MK for pgsql-jdbc@arkaria.postgresql.org; Tue, 15 Oct 2019 21:16:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iKUBF-0001vV-VJ for pgsql-jdbc@arkaria.postgresql.org; Tue, 15 Oct 2019 21:16:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iKUBF-0001vM-Lu for pgsql-jdbc@lists.postgresql.org; Tue, 15 Oct 2019 21:16:33 +0000 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iKUBC-0000f5-Or for pgsql-jdbc@lists.postgresql.org; Tue, 15 Oct 2019 21:16:33 +0000 Received: by mail-pg1-x52b.google.com with SMTP id 23so12913444pgk.3 for ; Tue, 15 Oct 2019 14:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=h70q+Uex5lxKnoroOWBDHFD6Tz5S0I1PI5Bm/Z6rqyg=; b=F1vEuxlZptTxKB65F3ZeQ4CmhfSVXZlAb6zlfHncZ280EktWJDo9pAjvzhc8hECU/y w9TkGxavvKzI2G8gMgXbYyMCQfCjUZzUXYTqXl6q3Bv25uz3Enh2frevpdSxEmRDSN7E u2IloUgpCeauUURHYqlDjgPdiVTS3MzRwkYQsDqlxbBuSy5vKT/YOJZXuA4DBgL15EIC RFo9QbPu9RdEpP66cvH8z6dHuUKOew1g3tIfwb7AZjMOLI9aK/MxlGdqs/bmuEB09+2t j3W8M69nKO27cmIvg9Nw1bt3QfLaUv/iW4x/MN6bnA3mJseXK/L1sLP/L6uGO1n6bY/c UxOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:message-id:date:to; bh=h70q+Uex5lxKnoroOWBDHFD6Tz5S0I1PI5Bm/Z6rqyg=; b=tTQuc7ypSMhTgDxrVlA+kPqIb369OC0HsL0FJi26/WIjp3dOUNTK6O32Y0mBJcKsIv v215GmNPTpOSy0g2Ij9e7HX2AwicVA7EQKAwEwt7DlP0unalTpKRs7jFLm4iYOOYoVS+ nnVIjEKrmLv6jMqg3E/N2AZujs3Qr2rkKZR+OdfS3CetOSXNgTRdFSEBdbEf6qWVabwP k2hIRHCe2/Y5UAO2mTNJ6tEDEZeQfxZsw8Iw+LIm5iUV9IjNtV8YGMD1QdPX+X6wW5Vz O+8WS8J2mZXI6ZPEmdGyxbbwaBF2tMOwVVHNaOP+vullmGeyPGjAYG1p/F7+cdqHOJoO JmGA== X-Gm-Message-State: APjAAAXQdrhTeFFi4RAXJjaz1uyE1TnWf+hcoeuMjF18QFMDf2m/9WUN 7z+KSnXi+opLrbS1qzttSA9e0qxEjt8= X-Google-Smtp-Source: APXvYqxNgC4zaDIx/p/Dn6olIT61KEgPwxc69a2S9jPuHgjiH3kDgrYuy3c1+WtTgWB71LqDzZh/Tg== X-Received: by 2002:a63:5d04:: with SMTP id r4mr40606614pgb.22.1571174187875; Tue, 15 Oct 2019 14:16:27 -0700 (PDT) Received: from [192.168.0.133] (c-76-22-40-96.hsd1.wa.comcast.net. [76.22.40.96]) by smtp.gmail.com with ESMTPSA id x12sm19997051pfm.130.2019.10.15.14.16.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Oct 2019 14:16:27 -0700 (PDT) From: Basil Bourque X-Google-Original-From: Basil Bourque Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: =?utf-8?Q?Configure_Apache_Tomcat=E2=80=99s_JNDI_to_return_a_=60D?= =?utf-8?Q?ataSource=60_of_class_=60PGSimpleDataSource=60_?= Message-Id: Date: Tue, 15 Oct 2019 14:16:26 -0700 To: pgsql-jdbc@lists.postgresql.org X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Question How do I tell Tomcat 9 to use a Postgres-specific object factory for = producing DataSource object in response to JNDI query? Details I have been wrestling Tomcat 9.0.26 to get its JNDI implementation to = generate a `DataSource` whose implementation is the class = `org.postgresql.ds.PGSimpleDataSource`, rather than the = `org.apache.tomcat.dbcp.dbcp2.BasicDataSource` class obtained by = default.=20 I am using the JNDI approach to externalize the database connection info = (username, password, etc.) as a config file for Tomcat. That = externalizing seems better for my needs than embedding those settings = inside my web-app=E2=80=99s WAR file. My steps: (1) Get the `postgresql-42.2.8.jar` on the appropriate class loader. (2) Write an XML file for `` with a `factory` attribute for an = implementation of `javax.naming.spi.ObjectFactory`. (3) Place that context definition file in correct location. (4) Register the `PGSimpleDataSource` class with Java=E2=80=99s Service = Provider Interface (SPI). (5) In my Java code, use a JNDI context to access a `DataSource` object. =E2=80=94--=E2=80=94| Step 1 |=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80= =94=E2=80=94=E2=80=94 In my designated =E2=80=9Cbase=E2=80=9D folder for Tomcat, in a `lib` = folder, place the `postgresql-42.2.8.jar` file.=20 Given Tomcat=E2=80=99s default `catalina.properties` file=E2=80=99s = settings, that *base*/lib location leads to the JDBC driver being loaded = in Tomcat=E2=80=99s =E2=80=9CCommon=E2=80=9D class loader. =E2=80=94--=E2=80=94| Step 2 |=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80= =94=E2=80=94=E2=80=94 Write an XML file named the same as my context. In this case: = `clepsydra.xml` for a context named `clepsydra`.=20 That file=E2=80=99s content: Note how I included an attribute for `factory` in that `Resource` = definition. I am only guessing that is the right thing to do, based on = my reading of the =E2=80=9CResource Definitions=E2=80=9D section of the = Tomcat page =E2=80=9CThe Context Container=E2=80=9D at: = https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Resource_Defi= nitions That page is confusing as it discusses only global resources, but I want = this resource only for my own web-app, not globally. My understanding is that `PGObjectFactory` implements = `javax.naming.spi.ObjectFactory`. This means I should be able to tell = Tomcat to use this object factory rather than Tomcat=E2=80=99s own = object factory. The goal is to get at runtime a = `org.postgresql.ds.PGSimpleDataSource` object rather than a = `org.apache.tomcat.dbcp.dbcp2.BasicDataSource`. =E2=80=94--=E2=80=94| Step 3 |=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80= =94=E2=80=94=E2=80=94 To place this XML file, I go to the `conf` folder I copied from Tomcat = into my designated Tomcat =E2=80=9Cbase=E2=80=9D folder. In that `conf` = folder, I create a `Catalina` folder for the name of the engine. Within = that I create a folder named `localhost` for the name of the host, as I = am running my Vaadin 14 web app from IntelliJ Ultimate edition 2019.3 = externally in Tomcat 9.0.26 in macOS Mojave with Java 13. I place my `clepsydra.xml` file in that *base*/conf/Catalina/localhost = folder. I know this context file is being loaded successfully because at runtime = I am able to access the Environment entry seen above in the XML file: ctxInitial =3D new InitialContext(); ctxEnv =3D ( Context ) ctxInitial.lookup( "java:comp/env" ); String deploymentModeString =3D ( String ) ctxEnv.lookup( = "work.basil.example.deployment-mode" ); value: DEV =E2=80=94--=E2=80=94| Step 4 |=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80= =94=E2=80=94=E2=80=94 I am guessing that given the `.spi.` in = `javax.naming.spi.ObjectFactory`, I need to register my desired = `PGObjectFactory` via Java Service Provider Interface (SPI) facility.=20 So in my Vaadin app=E2=80=99s `resources` folder I create a `META-INF` = folder. In there I create a `services` folder. In that = `resources/META-INF/services` folder I create a file named exactly the = name of the interface:=20 javax.naming.spi.ObjectFactory Inside that file I write one line, the name of the implementing class:=20= org.postgresql.ds.common.PGObjectFactory =E2=80=94--=E2=80=94| Step 5 |=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80= =94=E2=80=94=E2=80=94 After cleaning and rebuilding my project, at runtime I execute this = code: ctxInitial =3D new InitialContext(); DataSource dataSource =3D ( DataSource ) ctxInitial.lookup( = "java:comp/env/jdbc/postgres" ); System.out.println( "dataSource =3D " + dataSource ); value: null After all that effort, I get a null `DataSource`, with no Exception = thrown. Perhaps there are error messages, but I do not see any on the = console within IntelliJ. Is there somewhere else to look for error = messages? Can anyone tell me what I have missed, or what I am doing wrong? For another telling of this tale, see my Stack Overflow Question: https://stackoverflow.com/q/58385528/642706 =E2=80=94Basil Bourque