I want to connect a project in Java with hibernate and mySQL databases.
I rent a server to Ovh (kimsufi configure on ubuntu server and i configure mysql);
I can connect to phpmyadmin from distant computer 39...*/phpmyadmin , create databases tables ect...
But with hibernate I can't and I don't know why.
When i launch the application it raise an error :
WARN: SQL Error: 1130, SQLState: HY000
3 déc. 2013 13:09:45 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: null, message from server: "Host 'ALyon-654-1-427-15.w109-212.abo.wanadoo.fr' is not allowed to connect to this MySQL server"
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not open connection
Here is my hibernate cfg file :
<!-- <?xml version="1.0" encoding="UTF-8"?> -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">athos</property>
<property name="hibernate.connection.password">*******</property>
<property name="hibernate.connection.url">jdbc:mysql://37.*.*.*:3306</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">1</property>
<mapping class="fr.javatp.model.Client"></mapping>
</session-factory>
</hibernate-configuration>
The computer you're using to connect to the kimsufi server isn't allowed to connect as user athos to the mysql database. A detailed writeup can be found here. Essentially, you grant the appropriate rights with a GRANT command:
GRANT ALL ON yourdb.* TO athos#'1.2.3.4' IDENTIFIED BY 'my_password';
But that won't work with dynamic residential connections of course.
Alternatively, you could forward the traffic through an ssh tunnel between your machine and the kimsufi server, or even set up an OpenVPN server on the kimsufi server.
In my opinion, both these options are security-wise a LOT better than eg setting up phpMyAdmin.
Last but not least, add the database name to the JDBC URL:
jdbc:mysql://37.*.*.*:3306/yourdbnameshouldgohere
The error message is pretty clear
"Host 'ALyon-654-1-427-15.w109-212.abo.wanadoo.fr' is not allowed to
connect to this MySQL server".
It means that your host is not allowed to connect to the remote SQL database.
You need to allow your host to do remote connection via phpMyadmin (if it is possible with OVH. I'n not sure about that!)
http://supportcenter.verio.com/KB/questions.php?questionid=494
Related
Well, this question may be a duplicate or something. But I am desperate. I am learning Java EE and I cant even set up all the configurations properly.
My situation:
I have a Wildfly 8.1- Jboss server, up and ready.
I have deployed datasource (SQL server) Its working.
It has this JNDI:
java:/TimeTrackerDS
And this driver: sqlserver
Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver
All this seems to be okay. Now I am deploying my new Application.
And I just dont know how to use this staff to connect to database.
The application is also deployed. How to connect these 2 things (db and app)
I have found a lot of solutions. Write properties to persistance.xml But I cant get anything to work. Because I don't understand it. Maybe if someone from you guys, show it to me on my particular example. I would understand what I am doing wrong.
(The server, app and sql server are all at same machine )(localhost)
I found the solution.
I have a mistake in my entity java class. So it didnt compile properly.
Propper persistence.xml for someone who may need it :
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="TimeTracker">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/TimeTrackerDS</jta-data-source>
<class>model.Task</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.jdbc.batch_size" value="20" />
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
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
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
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%
I am trying to set up Netbeans to work with Hibernate using jtds driver to connect to SQLSERVER 2008.
The sqlserver 2008 is installed localy, no username or password.
This is hibernate.cfg:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:2545/MyDB</property>
</session-factory>
</hibernate-configuration>
When I try to setup Hibernate reverse engineering file with Hibernate reverse engineering wizard, I get the following error:
Unable to connect. Cannot establish a connection to
jdbc:jtds:sqlserver://localhost:2545/MyDB using
net.sourceforge.jtds.jdbc.Driver (I/O Error: SSO Failed: Native SSPI
library not loaded. Check the java.library.path system property.).
I have ntlmauth.dll in WINDOWS\system32.
jdbc:jtds:sqlserver://localhost:2545/MyDB appears to be an incomplete JDBC URL for the jTDS driver.
Going by the jTDS FAQ, you'll need to specify the username and password properties as well. Also, since you appear to be using integrated Windows authentication in MS SQL Server 2008, you would also need to set the domain property.
Note that, the username and password values will have to be provided unless you install the jTDS SSO library i.e. ntlmauth.dll; since you already have it in your PATH, you'll need to specify the domain property to login as the currently logged in user on the workstation.