Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I successfully connect to a Dockerized DB2 database/server from an app running on a dockerized Tomee server - both containers using jdk10?

How do I successfully connect to a Dockerized DB2 DB server from an app running on a dockerized Tomee server - both containers using jdk10?

What I had tried to do so far....

Dropped db2jcc4.jar into "/usr/local/tomee/lib" folder...

...obtained from:

    https://www-01.ibm.com/support/docview.wss?uid=swg21385217

Fwiw, the initial boot of tomcat server complained it couldn't find pdq.jar...:

    05-Sep-2018 16:48:04.901 INFO [localhost-startStop-1] org.apache.openejb.assembler.classic.Assembler.createApplication Deployed Application(path=C:\tools\apache-tomee-plume-7.0.5\webapps\docs)
    05-Sep-2018 16:48:04.956 WARNING [localhost-startStop-1] org.apache.tomcat.util.scan.StandardJarScanner.processURLs Failed to scan [file:/C:/tools/apache-tomee-plume-7.0.5/lib/pdq.jar] from classloader hierarchy
    java.io.IOException: java.lang.reflect.InvocationTargetException
            at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:212)
            at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:65)
            at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49)
            at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:374)
            at org.apache.tomcat.util.scan.StandardJarScanner.processURLs(StandardJarScanner.java:309)
            at org.apache.tomcat.util.scan.StandardJarScanner.doScanClassPath(StandardJarScanner.java:266)
            at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:229)
            at org.apache.tomee.loader.TomEEJarScanner.scan(TomEEJarScanner.java:69)
            at org.apache.catalina.startup.ContextConfig.processJarsForWebFragments(ContextConfig.java:1888)
            at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1116)
            at org.apache.catalina.startup.OpenEJBContextConfig.webConfig(OpenEJBContextConfig.java:411)
            at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:765)
            at org.apache.catalina.startup.OpenEJBContextConfig.configureStart(OpenEJBContextConfig.java:124)
            at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
            at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
           at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
            at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1140)
            at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
            at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514)
            at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
            at java.base/java.lang.Thread.run(Thread.java:844)
    Caused by: java.lang.reflect.InvocationTargetException
            at java.base/jdk.internal.reflect.GeneratedConstructorAccessor10.newInstance(Unknown Source)
            at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
            at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:209)
            ... 26 more
    Caused by: java.nio.file.NoSuchFileException: C:\tools\apache-tomee-plume-7.0.5\lib\pdq.jar
            at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
            at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
            at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
            at java.base/sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:53)
            at java.base/sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:38)
            at java.base/sun.nio.fs.WindowsFileSystemProvider.readAttributes(WindowsFileSystemProvider.java:194)
            at java.base/java.nio.file.Files.readAttributes(Files.java:1755)
            at java.base/java.util.zip.ZipFile$Source.get(ZipFile.java:1220)
            at java.base/java.util.zip.ZipFile$CleanableResource.<init>(ZipFile.java:727)
            at java.base/java.util.zip.ZipFile$CleanableResource.get(ZipFile.java:845)
            at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:245)
            at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:175)
            at java.base/java.util.jar.JarFile.<init>(JarFile.java:341)
            ... 30 more

...so I removed pdq.jar from the jar's manifest classpath and the tomee/tomcat server booted clean thereafter.

I deployed a simple REST app - containing a GET method - that utilizes a simple jdbc call to extract data from the IBM "SAMPLE" database (that is bundled with db2express-c)

The application looks like this...

    package aaa.bbb.ccc.war;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.Deque;
    import java.util.List;

    import javax.annotation.Resource;
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.TypedQuery;
    import javax.persistence.criteria.CriteriaBuilder;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Predicate;
    import javax.persistence.criteria.Root;
    import javax.sql.DataSource;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.UriInfo;

    import aaa.bbb.ccc.generated.EmployeeList;
    import aaa.bbb.ccc.generated.EmployeeType;
    import aaa.bbb.ccc.generated.ObjectFactory;

    @Stateless
    @Path("/employeeList")
    public class MyRestSvc {

        @Context
        UriInfo uriInfo;

        public MyRestSvc() {
        }

        @Resource(name = "jdbc/sample", type = javax.sql.DataSource.class)  
        private DataSource sampleDb;

        @GET
        @Path("{empno}")
        @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
        public EmployeeList get(@Context javax.servlet.http.HttpServletRequest request, @PathParam("empno") String empno) {
            String empNo = null;
            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;

            try {
                con = sampleDb.getConnection();
                con.setAutoCommit(false);

                // Create the Statement
                stmt = con.createStatement();
                System.out.println("**** Created JDBC Statement object");

                // Execute a query and generate a ResultSet instance
                rs = stmt.executeQuery("SELECT EMPNO FROM EMPLOYEE");
                System.out.println("**** Created JDBC ResultSet object");

                // Print all of the employee numbers to standard output device
                while (rs.next()) {
                    empNo = rs.getString(1);
                    System.out.println("Employee number = " + empNo);
                }

                System.out.println("**** Fetched all rows from JDBC ResultSet");
                // Close the ResultSet
                rs.close();
                System.out.println("**** Closed JDBC ResultSet");

                // Close the Statement
                stmt.close();
                System.out.println("**** Closed JDBC Statement");

                // Connection must be on a unit-of-work boundary to allow close
                con.commit();
                System.out.println("**** Transaction committed");

                // Close the connection
                con.close();


            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (con != null) {
                    System.out.println("Connected successfully.");
                    try {
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }

            return new EmployeeList();
        }
    }

I ran a test curl command to perform a GET operation... i.e.,

    curl -k -v -L -H "Content-Type: application/xml"   -X GET http://localhost:8888/MyRestSvc/employeeList/000190

and received this exception...

"Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused (Connection refused). ERRORCODE=-4499, SQLSTATE=08001 "

i.e.,

    -
    -
    -
    10-Sep-2018 21:04:15.767 INFO [main] org.apache.openejb.assembler.classic.Assembler.createRecipe Creating Resource(id=jdbc/sample)
    10-Sep-2018 21:04:16.173 SEVERE [main] org.apache.tomcat.jdbc.pool.ConnectionPool.init Unable to create initial connections of pool.
     com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.21.29] Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused (Connection refused). ERRORCODE=-4499, SQLSTATE=08001
            at com.ibm.db2.jcc.am.kd.a(kd.java:338)
            at com.ibm.db2.jcc.am.kd.a(kd.java:435)
            at com.ibm.db2.jcc.t4.ac.a(ac.java:440)
            at com.ibm.db2.jcc.t4.ac.<init>(ac.java:96)
            at com.ibm.db2.jcc.t4.a.b(a.java:366)
            at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2076)
            at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:812)
            at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:754)
            at com.ibm.db2.jcc.t4.b.<init>(b.java:339)
            at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
            at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199)
            at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:482)
            at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:116)
            at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
            at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEConnectionPool.<init>(TomEEDataSourceCreator.java:221)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEDataSource.createPool(TomEEDataSourceCreator.java:154)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEDataSource.<init>(TomEEDataSourceCreator.java:131)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator.pool(TomEEDataSourceCreator.java:68)
            at org.apache.openejb.resource.jdbc.pool.PoolDataSourceCreator.poolManaged(PoolDataSourceCreator.java:80)
            at org.apache.openejb.resource.jdbc.DataSourceFactory.create(DataSourceFactory.java:213)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:564)
            at org.apache.xbean.recipe.ReflectionUtil$StaticFactory.create(ReflectionUtil.java:999)
            at org.apache.xbean.recipe.ObjectRecipe.internalCreate(ObjectRecipe.java:276)
            at org.apache.xbean.recipe.AbstractRecipe.create(AbstractRecipe.java:96)
            at org.apache.xbean.recipe.AbstractRecipe.create(AbstractRecipe.java:61)
            at org.apache.openejb.assembler.classic.Assembler.doCreateResource(Assembler.java:3131)
            at org.apache.openejb.assembler.classic.Assembler.createResource(Assembler.java:2964)
            at org.apache.openejb.assembler.classic.Assembler.buildContainerSystem(Assembler.java:586)
            at org.apache.openejb.assembler.classic.Assembler.build(Assembler.java:487)
            at org.apache.openejb.OpenEJB$Instance.<init>(OpenEJB.java:150)
            at org.apache.openejb.OpenEJB.init(OpenEJB.java:307)
            at org.apache.tomee.catalina.TomcatLoader.initialize(TomcatLoader.java:247)
            at org.apache.tomee.catalina.ServerListener.lifecycleEvent(ServerListener.java:168)
            at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
            at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:395)
            at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:108)
            at org.apache.catalina.startup.Catalina.load(Catalina.java:632)
            at org.apache.catalina.startup.Catalina.load(Catalina.java:655)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:564)
            at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:309)
            at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)
    Caused by: java.net.ConnectException: Connection refused (Connection refused)
            at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
            at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
            at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
            at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)
            at java.base/java.net.Socket.connect(Socket.java:591)
            at com.ibm.db2.jcc.t4.w.run(w.java:49)
            at java.base/java.security.AccessController.doPrivileged(Native Method)
            at com.ibm.db2.jcc.t4.ac.a(ac.java:426)
            ... 49 more

    10-Sep-2018 21:04:16.192 SEVERE [main] org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEDataSource.<init> Can't create DataSource
     com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.21.29] Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused (Connection refused). ERRORCODE=-4499, SQLSTATE=08001
            at com.ibm.db2.jcc.am.kd.a(kd.java:338)
            at com.ibm.db2.jcc.am.kd.a(kd.java:435)
            at com.ibm.db2.jcc.t4.ac.a(ac.java:440)
            at com.ibm.db2.jcc.t4.ac.<init>(ac.java:96)
            at com.ibm.db2.jcc.t4.a.b(a.java:366)
            at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2076)
            at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:812)
            at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:754)
            at com.ibm.db2.jcc.t4.b.<init>(b.java:339)
            at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
            at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199)
            at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:482)
            at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:116)
            at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
            at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
            at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEConnectionPool.<init>(TomEEDataSourceCreator.java:221)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEDataSource.createPool(TomEEDataSourceCreator.java:154)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEDataSource.<init>(TomEEDataSourceCreator.java:131)
            at org.apache.tomee.jdbc.TomEEDataSourceCreator.pool(TomEEDataSourceCreator.java:68)
            at org.apache.openejb.resource.jdbc.pool.PoolDataSourceCreator.poolManaged(PoolDataSourceCreator.java:80)
            at org.apache.openejb.resource.jdbc.DataSourceFactory.create(DataSourceFactory.java:213)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:564)
            at org.apache.xbean.recipe.ReflectionUtil$StaticFactory.create(ReflectionUtil.java:999)
            at org.apache.xbean.recipe.ObjectRecipe.internalCreate(ObjectRecipe.java:276)
            at org.apache.xbean.recipe.AbstractRecipe.create(AbstractRecipe.java:96)
            at org.apache.xbean.recipe.AbstractRecipe.create(AbstractRecipe.java:61)
            at org.apache.openejb.assembler.classic.Assembler.doCreateResource(Assembler.java:3131)
            at org.apache.openejb.assembler.classic.Assembler.createResource(Assembler.java:2964)
            at org.apache.openejb.assembler.classic.Assembler.buildContainerSystem(Assembler.java:586)
            at org.apache.openejb.assembler.classic.Assembler.build(Assembler.java:487)
            at org.apache.openejb.OpenEJB$Instance.<init>(OpenEJB.java:150)
            at org.apache.openejb.OpenEJB.init(OpenEJB.java:307)
            at org.apache.tomee.catalina.TomcatLoader.initialize(TomcatLoader.java:247)
            at org.apache.tomee.catalina.ServerListener.lifecycleEvent(ServerListener.java:168)
            at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
            at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:395)
            at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:108)
            at org.apache.catalina.startup.Catalina.load(Catalina.java:632)
            at org.apache.catalina.startup.Catalina.load(Catalina.java:655)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:564)
            at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:309)
            at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)
    Caused by: java.net.ConnectException: Connection refused (Connection refused)
            at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
            at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
            at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
            at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)
            at java.base/java.net.Socket.connect(Socket.java:591)
            at com.ibm.db2.jcc.t4.w.run(w.java:49)
            at java.base/java.security.AccessController.doPrivileged(Native Method)
            at com.ibm.db2.jcc.t4.ac.a(ac.java:426)
            ... 49 more
    -
    -
    -

Researching, found this link...:

    http://www-01.ibm.com/support/docview.wss?uid=swg21287078

...however, the "solution" seems to assume that both the tomee server and db2 server were on the same machine(?)...

Also, fwiw...

Here is the tomee/tomcat server.xml...

    <?xml version="1.0" encoding="UTF-8"?>
    <Server port="8005" shutdown="SHUTDOWN">
        <!-- TomEE plugin for Tomcat -->
        <Listener className="org.apache.tomee.catalina.ServerListener" />
        <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
        <!--APR library loader. Documentation at /docs/apr.html -->
        <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
        <!-- Prevent memory leaks due to use of particular java/javax APIs-->
        <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
        <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
        <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

        <GlobalNamingResources>
            <Resource name="UserDatabase" auth="Container"
                      type="org.apache.catalina.UserDatabase"
                      description="User database that can be updated and saved"
                      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                      pathname="conf/tomcat-users.xml" />
        </GlobalNamingResources>

        <Service name="Catalina">

            <Connector port="8084" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" xpoweredBy="false" server="Apache TomEE" />
            <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

            <Engine name="Catalina" defaultHost="localhost">
                <Realm className="org.apache.catalina.realm.LockOutRealm">
                    <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
                </Realm>
                <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
                    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
                </Host>
            </Engine>
        </Service>
    </Server>

Here is the tomee/tomcat tomee.xml...

    <?xml version="1.0" encoding="UTF-8"?>
    <tomee>
            <Resource id="jdbc/sample" type="javax.sql.DataSource">
                    driverClassName = com.ibm.db2.jcc.DB2Driver
                    jdbcDriverType = 4
                    url = jdbc:db2://localhost:50000/SAMPLE
                    username = DB2INST1
                    password = mydb2-pwd
            </Resource>
    </tomee>

Dockerfile to generate the apache-tomee-plume-7.0.5 docker container...

    FROM openjdk:10-jre

    ENV PATH /usr/local/tomee/bin:$PATH
    RUN mkdir -p /usr/local/tomee

    WORKDIR /usr/local/tomee

    # curl -fsSL 'https://www.apache.org/dist/tomee/KEYS' | awk -F ' = ' '$1 ~ /^ +Key fingerprint$/ { gsub(" ", "", $2); print $2 }' | sort -u
    ENV GPG_KEYS \
        223D3A74B068ECA354DC385CE126833F9CF64915 \
        678F2D98F1FD9643811639FB622B8F2D043F71D8 \
        7A2744A8A9AAF063C23EB7868EBE7DBE8D050EEF \
        82D8419BA697F0E7FB85916EE91287822FDB81B1 \
        9056B710F1E332780DE7AF34CBAEBE39A46C4CA1 \
        A57DAF81C1B69921F4BA8723A8DE0A4DB863A7C1 \
        B7574789F5018690043E6DD9C212662E12F3E1DD \
        B8B301E6105DF628076BD92C5483E55897ABD9B9 \
        BDD0BBEB753192957EFC5F896A62FC8EF17D8FEF \
        C23A3F6F595EBD0F960270CC997C8F1A5BE6E4C1 \
        D11DF12CC2CA4894BDE638B967C1227A2678363C \
        DBCCD103B8B24F86FFAAB025C8BB472CD297D428 \
        F067B8140F5DD80E1D3B5D92318242FE9A0B1183 \
        FAA603D58B1BA4EDF65896D0ED340E0E6D545F97

    RUN set -xe \
        && for key in $GPG_KEYS; do \
            gpg --keyserver pgp.mit.edu --keyserver-options http-proxy=proxy.apps.dhs.gov:80 --recv-keys "$key" || \
            gpg --keyserver keyserver.pgp.com --keyserver-options http-proxy=proxy.apps.dhs.gov:80 --recv-keys "$key" || \
            gpg --keyserver ha.pool.sks-keyservers.net --keyserver-options http-proxy=proxy.apps.dhs.gov:80 --recv-keys "$key" ; \  
        done

    RUN set -x \
        && curl -fSL https://repo.maven.apache.org/maven2/org/apache/tomee/apache-tomee/7.0.5/apache-tomee-7.0.5-plume.tar.gz.asc --proxy proxy.apps.dhs.gov:80 -o tomee.tar.gz.asc \
        && curl -fSL https://repo.maven.apache.org/maven2/org/apache/tomee/apache-tomee/7.0.5/apache-tomee-7.0.5-plume.tar.gz --proxy proxy.apps.dhs.gov:80 -o tomee.tar.gz \
        && gpg --batch --verify tomee.tar.gz.asc tomee.tar.gz \
        && tar -zxf tomee.tar.gz \
        && mv apache-tomee-plume-7.0.5/* /usr/local/tomee \
        && rm -Rf apache-tomee-plume-7.0.5 \
        && rm bin/*.bat \
        && rm tomee.tar.gz*

    EXPOSE 8084
    CMD ["catalina.sh", "run"]

Dockerized DB2 from:

    https://hub.docker.com/r/ibmcom/db2express-c/

Tomee startup:

    root@5f9812df3398:/usr/local/tomee/bin# sh startup.sh
    ./catalina.sh: 165: ./catalina.sh: /docker-java-home=/docker-java-home: not found
    Using CATALINA_BASE:   /usr/local/tomee
    Using CATALINA_HOME:   /usr/local/tomee
    Using CATALINA_TMPDIR: /usr/local/tomee/temp
    Using JRE_HOME:        /docker-java-home
    Using CLASSPATH:       /usr/local/tomee/bin/bootstrap.jar:/usr/local/tomee/bin/tomcat-juli.jar
    Tomcat started.

Environment:

openjdk 10 (running in tomee/db2 docker containers)

tomee-plume-7.0.5 (dockerized)

db2 11.x (dockerized)

like image 721
sairn Avatar asked Sep 06 '18 20:09

sairn


People also ask

How does Db2 connect to IBM database?

Connect to an IBM Db2 database from Power Query DesktopSelect the IBM Db2 database option from Get Data. Specify the IBM Db2 server to connect to in Server. If a port is required, specify it by using the format ServerName:Port, where Port is the port number.

What is a container in Db2?

In a single container deployment, the database software and all of its component parts reside within one container. The container sits on one system and resources are shared by all the containerized components. An example of a single container deployment would be the Db2 Community Edition for Docker.


1 Answers

You are trying to connect to db with ip as localhost from tomcat container. But actually the db is separate container. So either you have to use alias of the db machine or connect it to host either in host networking mode or bridge mode with port forwarding and then you have to use static ip of host machine to connect it as either of follows from tomcat.

  1. jdbc:db2://<host-machine-static-ip>:50000/SAMPLE
  2. jdbc:db2://<docker-container-ip>:50000/SAMPLE
  3. jdbc:db2://<docker-container-alias>:50000/SAMPLE

Let me explain you in brief.

My Assumptions:

  1. Tomcat server and db servers are running as different containers.
  2. Both are connected to different docker networks.
  3. You have not port forwarded in db server.
  4. You haven't defined docker-compose file for the containers.
  5. You are running the containers using docker run.

If all of my assumptions are proper then there is a problem in docker network config. Basically there are different ways you can connect from one container to another container.

  1. By defining aliases for each container and connecting it to same network (recommended).
  2. By port forwarding from host to docker container and connecting to that port with host machine's static ip.
  3. By running the containers in host network and connecting to required port using host machine's static ip.
  4. By defining a static ip for docker container and connecting from other containers using that ip.

The recommended way in brief

You have to define alias for the container, For this you can either use the service name or you can declare additional alias as well. Then both should be connected to same network then only docker service discovery will work. Once every thing is done you can connect from one container to another container using alias of the other machine.

References:

  1. Work with network commands
  2. Use bridge networks
  3. Docker-compose alias
  4. Docker run reference
like image 111
Mani Avatar answered Sep 29 '22 04:09

Mani