jdbc sql server connection error on dev server - 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

Related

How to connect SQL server database with JPA project - Wildfly server - Java EE

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>

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

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

EclipseLink 2.5 not consulting persistence.xml

I created a Struts 2 web app using a maven archetype. Now, when I deployed that project on tomcat7 and edited persistence.xml to change database config, it is not reflecting in Application. To test my theory, I removed persistence.xml from WEB-INF/classes/META-INF/. Even then what I am getting is this:-
Connection refused: connect
java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect. Error Code: 40000
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect. Error Code: 40000
I don't have any idea from what location it is fetching the Persistence configuration. Here is my persistence.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<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="em" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.ankitsoft.entities.City</class>
<class>com.ankitsoft.entities.Country</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:C:\proLoc\WEB-INF\DB"/>
<property name="javax.persistence.jdbc.password" value="ankit"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value="ankit"/>
</properties>
</persistence-unit>
</persistence>
Can anybody please help me?
If you are calling the persistence.xml in a bundle, then org.eclipse.persistence.jpa must be started before your bundle (or bundles). It is therefore a good idea to always add org.eclipse.persistence as an imported package in your manifest

JPA MySQL Connection in Java EE project

I am running a local MySQL Server, later I want to use one on a remote computer and I have a question how to setup JPA in my Java EE project correctly.
First of all, do I have to setup the database connection in my application server or only in my persistence.xml? I have just added it in a JBoss Server and I can use JNDI now. But JNDI is not compatible with JPA, is it correct?
I tried it now with Geronimo and JBoss, with both I am not sure what I have to do correctly. I wanted to use OpenJPA as an JPA implementation which is standard on Geronimo.
I found many tutorials on the web, but most isnt very complet.
<?xml version="1.0" encoding="UTF-8"?>
<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="schulungen" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>MySQLServer</jta-data-source>
<class>model.Schulung</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
</properties>
</persistence-unit>
</persistence>
What is JTA-Data-Source? If I understood it right, it's a name given by the application server to identify the which connection I want.
Do I have to add properties like jdbc connection URL etc.?
I hope you understand my problem.

Connection mySQL and hibernate problems

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

Categories