'How to add Google Cloud MySQL libs to Tomcat 8.5
We want to deploy our Applications (.war-Files) via Tomcat 8.5 into a k8s-infrastructure, having the depending Database on Google Cloud SQL (MySQL).
Google offers the MySQL-Connector-Librarys ( see https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/blob/main/docs/jdbc-mysql.md & https://cloud.google.com/sql/docs/mysql/connect-connectors), sadly with only documentation about implementation into the java application itself. for company strategic reasons we don't want to do that.
We already tried to use a fat jar with all the dependencies of the mysql-connector and put that (together with the connectorJ/8.jar) into the tomcat/libs/ directory and configured the resource in the server.xml as follows:
<Resource name="jdbc/cloudSQL" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000"
factory="com.google.cloud.sql.mysql.SocketFactory" driverClassName="com.mysql.cj.jdbc.Driver" removeAbandonedOnBorrow="true" removeAbandonedTimeout="60" logAbandoned="true"
username="${db.user}" password="${db.pass}" url="jdbc:mysql:///${db.name}?cloudSqlInstance=${db.instance}&autoReconnect=true"
testOnBorrow="true" validationQuery="SELECT 1" />
Doesn't work too bad, but is not successfully either:
javax.naming.NamingException: Could not create resource factory instance
[Root exception is java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to javax.naming.spi.ObjectFactory]
at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:86)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:332)
at org.apache.naming.NamingContext.lookup(NamingContext.java:846)
at org.apache.naming.NamingContext.lookup(NamingContext.java:157)
at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:115)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:69)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:32)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:138)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:145)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:112)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:87)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:787)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:695)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476)
Caused by: java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to javax.naming.spi.ObjectFactory
at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:75) ... 22 more
I'd appreciate any idea how to tackle this issue.
Solution 1:[1]
I replicate the issue on Google Cloud, then instead of using the library from Google Cloud I was able to connect to Cloud SQL using Connector/J following the Tomcat instructions (https://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pablo |