Cannot load JDBC driver class 'com.mysql.jdbc.GoogleDriver' - java

I am facing a very bizarre problem where my App Engine server can't load its Cloud SQL's GoogleDriver, here's the error (It happens right after I run "mvn appengine:update").
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
...
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.GoogleDriver'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 48 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.GoogleDriver
appengine.properties
################### MySQL Configuration - Google Cloud App Engine ##########################
jdbc.driverClassName=com.mysql.jdbc.GoogleDriver
jdbc.url=jdbc:google:mysql://mytestapp:testdb?user=someuser
jdbc.username=someuser
jdbc.password=******
jdbc.dialect=org.hibernate.dialect.MySQLDialect
In my Spring context file I have:
<context:property-placeholder location="classpath:appengine.properties" />
<bean
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" >
<property
name="driverClassName"
value="${jdbc.driverClassName}" />
<property
name="url"
value="${jdbc.url}" />
...
Any ideas?
==
Just to confirm, I had already configured my appengine-web.xml and it doesn't help at all:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>mytestapp</application>
<version>1</version>
<threadsafe>true</threadsafe>
<sessions-enabled>true</sessions-enabled>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<use-google-connector-j>true</use-google-connector-j>
</appengine-web-app>
--
I couldn't move forward due to some incompatibility between hibernate jar versions so I kept trying through the actual release pipeline (Jenkins + Maven + build + test + deploy), I had to add the Compute Engine VM's IP address to the list of authorized IPs of the Cloud SQL Instance in order to run my unit tests and deploy it to the application (but, if I keep this approach, I can only use the MySQL driver and URL instead of the GoogleDriver and URL. So this is getting tricky...).
I have these properties in a Production.properties file that is loaded by my Spring MVC config file:
################### MySQL Configuration - Google Cloud App Engine ##########################
jdbc.driverClassName=com.mysql.jdbc.GoogleDriver
jdbc.url=jdbc:google:mysql://*******testapp:testsqldb?user=root
jdbc.username=root
jdbc.password=*****
jdbc.dialect=org.hibernate.dialect.MySQLDialect
Is there an easy way to dynamically switch between external and GAE Cloud SQL connection details without using this approach?
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
// Connecting from App Engine.
Any ideas?

The class is not automatically available in the App Engine runtime. You must enable it by adding <use-google-connector-j>true</use-google-connector-j> to your appengine-web.xml. This is documented at: https://cloud.google.com/appengine/docs/java/cloud-sql/#enable_connector_j
Edited to add (from comments below):
Also, when connecting to Cloud SQL from GAE you should leave the password field empty.
You should also make sure that if your code is running outside GAE (e.g. on your workstation, on GCE, on a Jenkins build) it uses stock MySQL connector as the Google connector is only available on GAE.
You might also want too look into using the stock MySQL driver, which works both from GAE and other connections. There is a demo of this at https://github.com/GoogleCloudPlatform/appengine-cloudsql-native-mysql-hibernate-jpa-demo-java

Related

Create Portable JDBC Connection Pool in Glassfish

I'm trying to deploy my project to AWS Elastic Beanstalk, using Docker with preconfigured Glassfish.
Since the project uses JPA, and EJB to create a transactional persistence unit; I create a database connection pool in the Glassfish admin console, then supply the JNDI string to persistence.xml.
All good in my local machine, Amazon won't allow you to use the admin console. I've read this post, which discusses generally how to configure Glassfish with asadmin, but I've found it very hard to follow and sounds very invasive.
Is there a one-size-fits-all solution to create a database connection pool that is automatically allocated on any server I deploy my application to?
Yes. Although this is unfortunately under-documented.
You can create an application scoped connection pool, that Glassfish creates on the fly, when you depoly your war file to it. It will destroy it when undeployed. Though, it is only available to this application on the server.
Here's what you should do. Create a file named glassfish-resources.xml like this (for MySql):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" name="MySQLPool" res-type="javax.sql.DataSource">
<property name="user" value="someUser"></property>
<property name="password" value="aPassword"></property>
<property name="databaseName" value="aDatabase"></property>
<property name="serverName" value="some.string.you got.from.amazon.rds.amazonaws.com"></property>
<property name="portNumber" value="3306"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="MySQLPool" jndi-name="jdbc/MySQLPool"></jdbc-resource>
</resources>
And drop it into your WEB-INF directory. You should also place the database connection dependency jar in the lib directory.
Now since this isn't a global JNDI reference, as it is only available to this application, you should access it in persistence.xml with java:app prefix, like this:
<jta-data-source>java:app/jdbc/MySQLPool</jta-data-source>

Wildfly Data Persistence

I am currently working on a Java EE project and am working with the Wildfly server.
I have a Web project and EJB project which are deployed onto the Wildfly server.
I can save a user for example, but only for as long as the server is running.
There is no data persistence between server downtimes.
I have searched through the internet but couldn't find an answer.
My persistence.xml looks like this:
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/kitchensink-quickstart-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/kitchensink-quickstart-ds.xml -->
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
<value="true"/>
</properties>
If I want to persist any information, do i need to reconfigure this file?
I hope you can help me :)
Your problem is this line
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
Everytime when the wildfly starts up, JPA creates a new database model with an empty database.
Adjust your code to
<property name="hibernate.hbm2ddl.auto" value="update" />
You are using "ExampleDS" which is set up as H2 in-memory database by default. It therefore does not persist data between restarts on purpose (useful for development/testing). Go to wildfly's standalone/configuration/standalone.xml configuration file and search for "ExampleDS" in the "datasources" section. It should show:
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
where "mem" means in-memory. You can change "mem:test" to any write path, e.g.
<connection-url>jdbc:h2:~/test;DB_CLOSE_DELAY=-1</connection-url>
to use a H2 file-based database stored as "test" in your home-folder (assuming *nix).
You can also define additional databases (Postgresql, Oracle, etc) in the datasources-section.

Managed VM not running Cloud SQL Proxy

Launching a Managed VM with a Java application, the following appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>thmadmin-ben</application>
<version>master</version>
<threadsafe>true</threadsafe>
<vm>true</vm>
<manual-scaling>
<instances>1</instances>
</manual-scaling>
</appengine-web-app>
I don't appear to have a Cloud SQL proxy running on the managed VM. Do I need to add an app.yaml file or define something else in appengine-web.xml?
Per the documentation, you need to specify the instances which you will be connecting to as follows and redeploy your application.
When using appengine-web.xml, the syntax would be:
<beta-settings>
<setting name="cloud_sql_instances" value="<PROJECT-ID>:<REGION-NAME>:<SQL-INSTANCE-NAME>[, ...]" />
</beta-settings>
If this setting is not present, the Cloud SQL Proxy won't start.

jdbc sql server connection error on dev server

I am using tomee, JPA and find below the configuration:
tomee.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="dataSource" type="DataSource">
JdbcDriver com.microsoft.sqlserver.jdbc.SQLServerDriver
JdbcUrl jdbc:sqlserver://******:1433;databaseName=******
UserName ******
Password ******
JtaManaged true
</Resource>
</tomee>
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="app">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>dataSource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
This is working fine on my local. But when I tried to deploy my application to Dev Linux server, I am getting this exception WARN: HHH000342: Could not obtain connection to query metadata : The TCP/IP connection to the host ******, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
The only difference is localhost is windows machine and dev server is Linux machine. As far as I know for this there are no changes required in connection string except the host name.
I also tried with DB server IP address instead of host name. Now server is stuck at this point: INFO: HHH000130: Instantiating explicit connection provider:org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
Please give your suggestions.
I tried with jtds and it worked. So I think there might be some issues if we use sql jdbc driver in linux.
There was one more issue. When I tried with jtds, I got this exception: java.lang.UnsupportedClassVersionError: net/sourceforge/jtds/jdbc/Driver : Unsupported major.minor version 51.0. My local JDK was 1.7 and Dev server JDK was 1.6. And I tried to deploy the war generated from my local to Dev server. Once I changed server JDK to 1.7 this issue also got resolved.
There’s a Windows service that runs on the database server called the “SQL Server Browser” that needed to be restarted. The service was listed as running, but it wasn’t responding to queries. As soon as we restarted this service everything started working again. Here’s some more information on this service.
http://msdn.microsoft.com/en-us/library/hh510203.aspx

Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/domain

Hello I'm using the following
hibernate-core-4.1.2.Final.jar
mysql-connector-5.1.6.jar
Both can be found in my project lib directory.
I have the following hibernate.cg.xml configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Defines the SQL dialect used in Hiberante's application -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--Local Database Connection-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/domain</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="hbm2ddl.auto">validate</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="hibernate.search.default.directory_provider">ram</property>
</session-factory>
</hibernate-configuration>
and I'm getting the following exception.
Caused by: java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/domain at
java.sql.DriverManager.getConnection(DriverManager.java:604) at
java.sql.DriverManager.getConnection(DriverManager.java:190) at
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at
org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
at
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
... 145 more
I do not want to use JNDI do to the fact management wants to keep the app as portable as possible, so what am I missing to get this to work with jdbc? Am I required to do any kind of configurations to tomcat?
Tomcat Lib
Try putting mysql-connector-5.1.6.jar directly into the lib folder of tomcat and restarting it.
Have you tried calling the driver class?:
Class.forName("com.mysql.jdbc.Driver");
How to call it:
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
// error out
}
Connection con = DriverManager.getConnection(/*your connection query*/);
I may have the class wrong, but if com.mysql.jdbc.Driver doesn't work, you can also try com.mysql.JDBC or com.mysql.jdbc (basing off how SQLite calls it)
Did you edit the config to obscure the connection string?
Your hibernate config has a different database name than the error:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/domain</property>
Caused by: java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/etss
In terms of portability, I package the database driver with my war, so it is self contained. This makes for deployment across multiple environments much easier and if another developer wants to build and run locally, they just have to drop the war into Tomcat and go. Place the database driver in your WEB-INF/lib folder.
Also, in terms portability, I recommend JNDI... that way you do not have to edit your hibernate config file when you deploy it to another server and it can stay packaged in your war. You just add the JNDI reference in the Tomcat config.
The exception occurs because the mysql database driver is not on your classpath. Add it to your classpath to repair the issue. Since you are using tomcat you can simply add it to the tomcat/lib directory.
I would suggest putting your drivers in/at where you have place JDK's Extension directory. Please see:http://www3.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#zz-4.1
Once that is done I would encourage you to from your prompt type:
echo %CLASSPATH%

Categories