How to set Hibernate with sqlserver&jtds driver in Netbeans - java

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.

Related

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

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%

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

Programatically setting the db url/user/password in Hibernate

I need to centralize all settings for our Java web application in one .properties file. I can still have hibernate.cfg.xml for adding mappings to entity classes but I need to keep all of our settings for the database and custom paths in one .properties file.
Originally I kept my configs in hibernate.cfg.xml as follows....
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">my jdbc connection</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.current_session_context_class">managed</property>
<mapping class="myEntityClass"/>
</session-factory>
</hibernate-configuration>
Now I want to move"connection.url", "connection.username", and "connection.password" to my own .properties file. The code for creating my hibernate configuration class went from.
new AnnotationConfiguration().configure();
to
new AnnotationConfiguration()
.setProperty("connection.url", databaseUrl)
.setProperty("connection.username", databaseUser)
.setProperty("connection.password", databasePassword)
.configure();
Which seemed conceptually simple. Unfortunately I get the following error when I try to use my Hibernate Session that worked with the previous config.
The user must supply a JDBC connection
Any ideas? It seems to me that when Hibernate sees these properties missing in the hibernate.cfg.xml file it assumes all settings will be manually added and ignore the xml altogether.
From the Hibernate Reference Documentation:
3.3. JDBC connections
[...]
The following is an example
hibernate.properties file for c3p0:
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
Adapt it to suit your needs and put the hibernate.properties in the root of the class path (and remove the equivalent entries from the hibernate.cfg.xml as the XML configuration file overrides properties). So there is actually no need to change the following line:
new AnnotationConfiguration().configure();
Unless you really want a programmatic configuration of course.
But from the body of your question, moving to a .properties file is something else and you can rely on Hibernate: move the relevant properties from hibernate.cfg.xml to hibernate.properties.
Try setting following properties
properties.put("hibernate.connection.driver_class", "net.sourceforge.jtds.jdbc.Driver");
properties.put("hibernate.connection.url", "jdbc:jtds:sqlserver://test/dbname;SSL=REQUEST");
properties.put("hibernate.connection.username", "user");
properties.put("hibernate.connection.password", "password");
properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
of course this is for SQL Server so you would need to change driver to 'org.gjt.mm.mysql.Driver"'
and change dialect as well 'org.hibernate.dialect.MySQLInnoDBDialect'
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialize=true
spring.flyway.baseline-on-migrate=true
flyway.baseline-on-migrate: true
spring.flyway.baselineVersionAsString=2
spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=true

Categories