datasource duplication in tomee

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

datasource duplication in tomee

dmcletchie
Hi,

I'm kind of new to TomEE & App servers in general.  I Apologize in advance if I use some incorrect terminology.

I have set up a web application that connects to a database.
To connect to the database, I'm defining a resource in a context.xml file.  I'm not modifying server.xml.
I'm using the PSI-probe to manage/monitor things.

My problem is that I keep getting duplicate database connection pools.  Or, I think that I'm getting duplicate pools.
The probe app seems to list 3 Application Resources. but I think that 2 of them might be linked  I can see some logging around the linking. (I see lines with "Auto-linking resource-ref" in the log file)

To tried to divide & conquor, I think that narrowed it down to my context.xml.  I created a hello world web app, that has 2 files.  One file extends javax.ws.rs.core.Application, and the other file has a single jax.ws.rs resource with a single hello world method in it.  So, the app doesn't use the database.  it just has 1 method with a one line "hello world" method.

Then I created a context file, that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/foobar"
  jtaManaged="true"
auth="Container" 
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
            url="jdbc:oracle:thin:@HHHHHHHH:1521:ORCL"
            username="XXXXXXXXX"
            password="ZZZZZZZZZZZ"
            maxTotal="10"
            maxIdle="5"
            minIdle="0"
            initialSize="2"
            maxWaitMillis="1"
            />
</Context>
and put it in META-INF

I grabbed a clean download of apache-tomee-7.0.3-plus, so that the config files are all at their default.
I added by helloworld.jar.  I added the probe.war, for management.  I added ojdbc8.jar to the libs directory.
I modified tomcat-users.xml so that I could log into the manager & the probe app.

When Tomee is not running, and I look at my database, there are no session from my development machine.  When I start the app, there are 4 connections.  (initial size is 2, I have 4 connections, so I think that I have 2 dbcp pools).

If I increase my initialSize to 3 & restart the app, then I can see in the database that I have 6 sessions.  (It is an oracle database, so I can see the connections with the command "select count(*) from v$session where machine = 'developmentmachine';")

When I look in the probe, I can see 3 application resources:

APP              RESOURCE                                 USAGE   M   E   B   USER      URL                                  DESCRIPTION   TYPE          LNK   AUTH
/helloWorld openejb/Resource/jdbc/foobar                     10  2   0   XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL               tomcat-dbcp2        CONTAINER 
/helloWorld jdbc/foobar                                      10  2   0   XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL               tomcat-dbcp2        Container 
/helloWorld openejb/Resource/helloWorld/jdbc/foobar          10  2   0   XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL               tomcat-dbcp2        CONTAINER

the "E" column is "the number of established connections that can be reused".  so, it's reporting 6 (2 for each resource) but I think that I have 4, since that's what the database tells me.  The AUTH column isn't exactly the same, 2 are "CONTAINER" and 1 is "Container".  so maybe that's a clue to the linking?

So - I'm hoping that since this is such a minimal config, someone can tell me what I'm doing wrong.  Do I really have 2 different dbcp pools?  or do I have 1 or 3?  Why is the number of session in the database always double the initialSize number, even though my app is never using the database.

I'll attach the snippet of the log file.  The logging is all at the defaults, since I just grabbed this install.  I can repeat it with additional logging if that would help.

Thanks!

Doug

catalina.log.txt (14K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: datasource duplication in tomee

Romain Manni-Bucau
Hi

few things to check:

1. does psiprobe say if that's the same instances or not? JMX should give
you the information I think since it will only list root resources
2. why not using tomee.xml or reosurces.xml to define the datasources -
this is the recommanded way?


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-08-10 17:22 GMT+02:00 dmcletchie <[hidden email]>:

> Hi,
>
> I'm kind of new to TomEE & App servers in general.  I Apologize in advance
> if I use some incorrect terminology.
>
> I have set up a web application that connects to a database.
> To connect to the database, I'm defining a resource in a context.xml file.
> I'm not modifying server.xml.
> I'm using the PSI-probe to manage/monitor things.
>
> My problem is that I keep getting duplicate database connection pools.  Or,
> I think that I'm getting duplicate pools.
> The probe app seems to list 3 Application Resources. but I think that 2 of
> them might be linked  I can see some logging around the linking. (I see
> lines with "Auto-linking resource-ref" in the log file)
>
> To tried to divide & conquor, I think that narrowed it down to my
> context.xml.  I created a hello world web app, that has 2 files.  One file
> extends javax.ws.rs.core.Application, and the other file has a single
> jax.ws.rs resource with a single hello world method in it.  So, the app
> doesn't use the database.  it just has 1 method with a one line "hello
> world" method.
>
> Then I created a context file, that looks like this:
> <?xml version="1.0" encoding="UTF-8"?>
> <Context>
> <Resource name="jdbc/foobar"
>   jtaManaged="true"
> auth="Container"
> type="javax.sql.DataSource"
> driverClassName="oracle.jdbc.OracleDriver"
>             url="jdbc:oracle:thin:@HHHHHHHH:1521:ORCL"
>             username="XXXXXXXXX"
>             password="ZZZZZZZZZZZ"
>             maxTotal="10"
>             maxIdle="5"
>             minIdle="0"
>             initialSize="2"
>             maxWaitMillis="1"
>             />
> </Context>
> and put it in META-INF
>
> I grabbed a clean download of apache-tomee-7.0.3-plus, so that the config
> files are all at their default.
> I added by helloworld.jar.  I added the probe.war, for management.  I added
> ojdbc8.jar to the libs directory.
> I modified tomcat-users.xml so that I could log into the manager & the
> probe app.
>
> When Tomee is not running, and I look at my database, there are no session
> from my development machine.  When I start the app, there are 4
> connections.  (initial size is 2, I have 4 connections, so I think that I
> have 2 dbcp pools).
>
> If I increase my initialSize to 3 & restart the app, then I can see in the
> database that I have 6 sessions.  (It is an oracle database, so I can see
> the connections with the command "select count(*) from v$session where
> machine = 'developmentmachine';")
>
> When I look in the probe, I can see 3 application resources:
>
> APP              RESOURCE                                 USAGE   M   E   B
>   USER      URL                                  DESCRIPTION   TYPE
>  LNK   AUTH
> /helloWorld openejb/Resource/jdbc/foobar                     10  2   0
> XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL               tomcat-dbcp2
>      CONTAINER
> /helloWorld jdbc/foobar                                      10  2   0
> XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL               tomcat-dbcp2
>      Container
> /helloWorld openejb/Resource/helloWorld/jdbc/foobar          10  2   0
> XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL               tomcat-dbcp2
>      CONTAINER
>
> the "E" column is "the number of established connections that can be
> reused".  so, it's reporting 6 (2 for each resource) but I think that I
> have 4, since that's what the database tells me.  The AUTH column isn't
> exactly the same, 2 are "CONTAINER" and 1 is "Container".  so maybe that's
> a clue to the linking?
>
> So - I'm hoping that since this is such a minimal config, someone can tell
> me what I'm doing wrong.  Do I really have 2 different dbcp pools?  or do I
> have 1 or 3?  Why is the number of session in the database always double
> the initialSize number, even though my app is never using the database.
>
> I'll attach the snippet of the log file.  The logging is all at the
> defaults, since I just grabbed this install.  I can repeat it with
> additional logging if that would help.
>
> Thanks!
>
> Doug
>
>
> catalina.log.txt (14K) <http://tomee-openejb.979440.
> n4.nabble.com/attachment/4682413/0/catalina.log.txt>
>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/datasource-duplication-in-tomee-tp4682413.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: datasource duplication in tomee

jgallimore
Can you pop your sample app somewhere (as a GitHub project, maybe)? Happy
to deploy it and take a look here.

Jon

On Fri, Aug 11, 2017 at 11:28 AM, Romain Manni-Bucau <[hidden email]>
wrote:

> Hi
>
> few things to check:
>
> 1. does psiprobe say if that's the same instances or not? JMX should give
> you the information I think since it will only list root resources
> 2. why not using tomee.xml or reosurces.xml to define the datasources -
> this is the recommanded way?
>
>
> Romain Manni-Bucau
> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> <https://blog-rmannibucau.rhcloud.com> | Old Blog
> <http://rmannibucau.wordpress.com> | Github <https://github.com/
> rmannibucau> |
> LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
> <https://javaeefactory-rmannibucau.rhcloud.com>
>
> 2017-08-10 17:22 GMT+02:00 dmcletchie <[hidden email]>:
>
> > Hi,
> >
> > I'm kind of new to TomEE & App servers in general.  I Apologize in
> advance
> > if I use some incorrect terminology.
> >
> > I have set up a web application that connects to a database.
> > To connect to the database, I'm defining a resource in a context.xml
> file.
> > I'm not modifying server.xml.
> > I'm using the PSI-probe to manage/monitor things.
> >
> > My problem is that I keep getting duplicate database connection pools.
> Or,
> > I think that I'm getting duplicate pools.
> > The probe app seems to list 3 Application Resources. but I think that 2
> of
> > them might be linked  I can see some logging around the linking. (I see
> > lines with "Auto-linking resource-ref" in the log file)
> >
> > To tried to divide & conquor, I think that narrowed it down to my
> > context.xml.  I created a hello world web app, that has 2 files.  One
> file
> > extends javax.ws.rs.core.Application, and the other file has a single
> > jax.ws.rs resource with a single hello world method in it.  So, the app
> > doesn't use the database.  it just has 1 method with a one line "hello
> > world" method.
> >
> > Then I created a context file, that looks like this:
> > <?xml version="1.0" encoding="UTF-8"?>
> > <Context>
> > <Resource name="jdbc/foobar"
> >   jtaManaged="true"
> > auth="Container"
> > type="javax.sql.DataSource"
> > driverClassName="oracle.jdbc.OracleDriver"
> >             url="jdbc:oracle:thin:@HHHHHHHH:1521:ORCL"
> >             username="XXXXXXXXX"
> >             password="ZZZZZZZZZZZ"
> >             maxTotal="10"
> >             maxIdle="5"
> >             minIdle="0"
> >             initialSize="2"
> >             maxWaitMillis="1"
> >             />
> > </Context>
> > and put it in META-INF
> >
> > I grabbed a clean download of apache-tomee-7.0.3-plus, so that the config
> > files are all at their default.
> > I added by helloworld.jar.  I added the probe.war, for management.  I
> added
> > ojdbc8.jar to the libs directory.
> > I modified tomcat-users.xml so that I could log into the manager & the
> > probe app.
> >
> > When Tomee is not running, and I look at my database, there are no
> session
> > from my development machine.  When I start the app, there are 4
> > connections.  (initial size is 2, I have 4 connections, so I think that I
> > have 2 dbcp pools).
> >
> > If I increase my initialSize to 3 & restart the app, then I can see in
> the
> > database that I have 6 sessions.  (It is an oracle database, so I can see
> > the connections with the command "select count(*) from v$session where
> > machine = 'developmentmachine';")
> >
> > When I look in the probe, I can see 3 application resources:
> >
> > APP              RESOURCE                                 USAGE   M   E
>  B
> >   USER      URL                                  DESCRIPTION   TYPE
> >  LNK   AUTH
> > /helloWorld openejb/Resource/jdbc/foobar                     10  2   0
> > XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL
>  tomcat-dbcp2
> >      CONTAINER
> > /helloWorld jdbc/foobar                                      10  2   0
> > XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL
>  tomcat-dbcp2
> >      Container
> > /helloWorld openejb/Resource/helloWorld/jdbc/foobar          10  2   0
> > XXXXXXXXX jdbc:oracle:thin:@HHHHHHHH:1521:ORCL
>  tomcat-dbcp2
> >      CONTAINER
> >
> > the "E" column is "the number of established connections that can be
> > reused".  so, it's reporting 6 (2 for each resource) but I think that I
> > have 4, since that's what the database tells me.  The AUTH column isn't
> > exactly the same, 2 are "CONTAINER" and 1 is "Container".  so maybe
> that's
> > a clue to the linking?
> >
> > So - I'm hoping that since this is such a minimal config, someone can
> tell
> > me what I'm doing wrong.  Do I really have 2 different dbcp pools?  or
> do I
> > have 1 or 3?  Why is the number of session in the database always double
> > the initialSize number, even though my app is never using the database.
> >
> > I'll attach the snippet of the log file.  The logging is all at the
> > defaults, since I just grabbed this install.  I can repeat it with
> > additional logging if that would help.
> >
> > Thanks!
> >
> > Doug
> >
> >
> > catalina.log.txt (14K) <http://tomee-openejb.979440.
> > n4.nabble.com/attachment/4682413/0/catalina.log.txt>
> >
> >
> >
> >
> > --
> > View this message in context: http://tomee-openejb.979440.
> > n4.nabble.com/datasource-duplication-in-tomee-tp4682413.html
> > Sent from the TomEE Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: datasource duplication in tomee

dmcletchie
Thank you both for replying - Much appreciated!

Here's the war file, build with source (all 3 or 4 lines!).  It's only a few KB, so hopefully it'll let me attach it.

helloWorld.war

I don't have a great reason for picking context.xml.  I want to do this in a more standard / common / tested way, so I'll try to look into that & change it.

The only "gotcha" is that I was trying to obfuscate the database password, by encrypting it. The code to decrypt it is in each war file.  To decrypt, I was defining a factory in the resource in context.xml.  That factory points to a class that extends org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory.  So, I could not define the resource in a global way, since the global class loader does not know how to get to the factory in the war file.

I'd rather not define the resource in the war file, since we might want to change some of the parameters like the URL, username, pool size, etc. per server that we install on.  I don't want to have to edit war file, I'd rather have the configuration live outside of the war file.  (especially for upgrades, I want to make it easy to deploy new war files)

I do have options though - I guess that I could use a globally defined resource, and I could put the decrypt code in a jar in the libs directory.  Given that I want to obfuscate the password (I know that it's not super secure), what's my best bet for setting up a data source?  

I don't know if it's the same instance or not.  I'm poking around the mbeans with jvisialvm ... i'm a bit over my head.  
I have 2 entries in
Rouserce->Context -> localhost -> /helloWorld -> org.apache.tomee.catalina.TomcatResourceFactory
They are:
"openejb/Resource/helloWorld/jdbc/foobar" and
"openejb/Resource/jdbc/foobar"
there is e endtry in
Rouserce->Context -> localhost -> /helloWorld -> javax.sql.DataSource
it is
"jdbc/foobar"

I think that there are 2 instances.  If I set the min/max/initial size of the pool to 11 connections, I end up with 22 sessions in my database.  If I cut & paste the resource definition into server.xml (and leave context.xml empty), then I end up with 11 connections (which I expect)

Let me know what you think the path of least resistance is for me to obfuscate a password like this... I'll try to set it up that way.

Thanks again!

Doug
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: datasource duplication in tomee

jgallimore
Thanks! I'll take a look at that - ideally, using context.xml wouldn't
cause this, so it'll be interesting to see what it going on there. I would
normally define datasource in conf/tomee.xml. There is a mechanism built in
for ciphering password - take a look at the "Password and ciphering"
section on http://tomee.apache.org/admin/configuration/resources.html.

Hope that helps.

Jon

On Fri, Aug 11, 2017 at 12:48 PM, dmcletchie <[hidden email]> wrote:

> Thank you both for replying - Much appreciated!
>
> Here's the war file, build with source (all 3 or 4 lines!).  It's only a
> few
> KB, so hopefully it'll let me attach it.
>
> helloWorld.war
> <http://tomee-openejb.979440.n4.nabble.com/file/n4682435/helloWorld.war>
>
> I don't have a great reason for picking context.xml.  I want to do this in
> a
> more standard / common / tested way, so I'll try to look into that & change
> it.
>
> The only "gotcha" is that I was trying to obfuscate the database password,
> by encrypting it. The code to decrypt it is in each war file.  To decrypt,
> I
> was defining a factory in the resource in context.xml.  That factory points
> to a class that extends org.apache.tomcat.dbcp.dbcp2.
> BasicDataSourceFactory.
> So, I could not define the resource in a global way, since the global class
> loader does not know how to get to the factory in the war file.
>
> I'd rather not define the resource in the war file, since we might want to
> change some of the parameters like the URL, username, pool size, etc. per
> server that we install on.  I don't want to have to edit war file, I'd
> rather have the configuration live outside of the war file.  (especially
> for
> upgrades, I want to make it easy to deploy new war files)
>
> I do have options though - I guess that I could use a globally defined
> resource, and I could put the decrypt code in a jar in the libs directory.
> Given that I want to obfuscate the password (I know that it's not super
> secure), what's my best bet for setting up a data source?
>
> I don't know if it's the same instance or not.  I'm poking around the
> mbeans
> with jvisialvm ... i'm a bit over my head.
> I have 2 entries in
> Rouserce->Context -> localhost -> /helloWorld ->
> org.apache.tomee.catalina.TomcatResourceFactory
> They are:
> "openejb/Resource/helloWorld/jdbc/foobar" and
> "openejb/Resource/jdbc/foobar"
> there is e endtry in
> Rouserce->Context -> localhost -> /helloWorld -> javax.sql.DataSource
> it is
> "jdbc/foobar"
>
> I think that there are 2 instances.  If I set the min/max/initial size of
> the pool to 11 connections, I end up with 22 sessions in my database.  If I
> cut & paste the resource definition into server.xml (and leave context.xml
> empty), then I end up with 11 connections (which I expect)
>
> Let me know what you think the path of least resistance is for me to
> obfuscate a password like this... I'll try to set it up that way.
>
> Thanks again!
>
> Doug
>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/datasource-duplication-in-tomee-tp4682413p4682435.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: datasource duplication in tomee

dmcletchie
I think that it is working if I define the datasource in tomee.xml.  Thanks for you help.

I set it up with the the Static3DES cipher.  I'll try to swap it out for something more obfuscated.

I can't see the data source in psiprobe when I define it in tomee.xml.  I'm trying to debug it, I think that it's because PSI probe is looking for JMX beans in  Catalina -> Resource -> ... , and when it find a datasource defined in tomee.xml, the type attribute of that bean is org.apache.openejb.resource.jdbc.DataSourceFactory.  I don't think that it knows how to handle that.  

When I define the datasource in context.xml, the bean in Catalina -> Resource -> ... has the type attribute javax.sql.datasource, so the probe can deal with that.

This probably isn't the right forum, but do you guys know how I can monitor/manage the data source defined tomee.xml with psiprobe?  (or is there another tool?).

Since i'm in a bit over my head, I really need some tools to poke around.  I think that it's going to help me learn how to use this stuff.

Thanks again for all of the help!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: datasource duplication in tomee

dmcletchie
I solved my problem with the PSI probe & I wanted to post this in case anyone comes across this issue.

If I define a datasource in tomee.xml, then it does not appear in the "Data Sources" tab in PSI probe.

The problem is that psi probe is looking for instances of "org.apache.tomcat.jdbc.pool.DataSource", and tomee is subclassing that, using "org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEDataSource".

I created an issue with PSI probe, and i'll submit a patch if i can figure out how to use github.  There's about 10 ways to solve it, but if anyone is stuck with this issue, this is a simple patch that functions as a workaround for me:
----------------------------------
a/core/src/main/java/psiprobe/beans/accessors/TomcatJdbcPoolDatasourceAccessor.java
+++ b/core/src/main/java/psiprobe/beans/accessors/TomcatJdbcPoolDatasourceAccessor.java
@@ -43,8 +43,8 @@ public class TomcatJdbcPoolDatasourceAccessor implements DatasourceAccessor {

   @Override
   public boolean canMap(Object resource) {
-    return "org.apache.tomcat.jdbc.pool.DataSource".equals(resource.getClass().getName())
-        && resource instanceof DataSource;
+    return /*"org.apache.tomcat.jdbc.pool.DataSource".equals(resource.getClass().getName())
+        &&*/ resource instanceof DataSource;
   }

 }

----------------------------------
Loading...