Hello Everyone,
I'm new to Hibernate, I tried the short tutorial on Hibernate (on this site) and was successful with connecting to HSQL DB.
However, now I'm trying to connect to MySQL version 4.0.20 , using
JDBC mysql-connector-java-3.1.14-bin.jar
I get org.hibernate.exception.JDBCConnectionException: Cannot open connection
I tried to connect to the database with plain old JDBC and was able to connect to MySQL with JDBC alone.
Here's my JDBC connection code:
Please note that I've used some pseudonyms for DB connection string in this illustration, for security reasons.
Code:
System.out.println("Testing MySql DB connection");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://192.168.1.xxx:3306/someapp";
Connection cn =
DriverManager.getConnection(url, "someapp", "someapp");
Statement st =
cn.createStatement();
ResultSet rs =
st.executeQuery("SELECT * FROM finapps.emp_test");
System.out.println("field_id: field_name, field_type, field_len, field_description");
while (rs.next()) {
/*System.out.println(rs.getInt("field_id") + ": "
+ rs.getString("field_name") + ", "
+ rs.getString("field_type") + ","
+ rs.getInt("field_len") + ","
+ rs.getString("field_description"));
*/
System.out.println(" Employee Name: " + rs.getString("emp_name"));
}
Hibernate version: 3.0
Mapping documents:Code:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--
<property name="connection.url">jdbc:mysql://192.168.1.xxx:3306/someapp</property>
-->
name="connection.url">jdbc:mysql://192.168.1.xxx:3306/someapp</property>
<property name="connection.username">someapp</property>
<property name="connection.password">someapp</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- TODO: Configure this for production -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!--
<property name="hbm2ddl.auto">update</property>
-->
<!-- Example -->
<!--
<mapping resource="events/Event.hbm.xml"/>
-->
<mapping resource="com/forbes/finapps/ibes/beans/identification/IbesIdentification.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
SomeBean someBean;
Iterator<SomeBean> it = someBeanList.iterator();
while (it.hasNext()) {
someBean = it.next();
session.save(someBean);
}
session.getTransaction().commit();
Full stack trace of any exception that occurs:
org.apache.jasper.JasperException: Cannot open connection
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
org.hibernate.exception.JDBCConnectionException: Cannot open connection
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:424)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
$Proxy0.beginTransaction(Unknown Source)
com.forbes.finapps.ibes.beans.identification.IbesIdentificationManager.createAndStoreIbesIdentification(IbesIdentificationManager.java:108)
com.forbes.finapps.ibes.beans.identification.IbesIdentificationManager.generateDummyIdentificationRecords(IbesIdentificationManager.java:92)
org.apache.jsp.jsp.test.StoreIbesIdentification_jsp._jspService(StoreIbesIdentification_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Name and version of the database you are using:
MySQL
mysql Ver 12.22 Distrib 4.0.20, for pc-linux (i686)
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Mar 1, 2007 10:58:18 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.
Mar 1, 2007 10:58:18 AM org.apache.coyote.http11.Http11BaseProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Mar 1, 2007 10:58:18 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 672 ms
Mar 1, 2007 10:58:19 AM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Mar 1, 2007 10:58:19 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.5.20
Mar 1, 2007 10:58:19 AM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
Mar 1, 2007 10:58:20 AM org.apache.coyote.http11.Http11BaseProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Mar 1, 2007 10:58:20 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Mar 1, 2007 10:58:20 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/31 config=null
Mar 1, 2007 10:58:20 AM org.apache.catalina.storeconfig.StoreLoader load
INFO: Find registry server-registry.xml at classpath resource
Mar 1, 2007 10:58:20 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1609 ms
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.2
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Mar 1, 2007 10:58:30 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Mar 1, 2007 10:58:30 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 1
Mar 1, 2007 10:58:30 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
Mar 1, 2007 10:58:30 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:hsqldb:hsql://localhost
Mar 1, 2007 10:58:30 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=finapps, password=****}
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
WARNING: Could not obtain connection metadata
java.sql.SQLException: No suitable driver found for jdbc:hsqldb:hsql://localhost
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at com.forbes.finapps.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
at com.forbes.finapps.ibes.beans.identification.IbesIdentificationManager.createAndStoreIbesIdentification(IbesIdentification
at com.forbes.finapps.ibes.beans.identification.IbesIdentificationManager.generateDummyIdentificationRecords(IbesIdentificati
at org.apache.jsp.jsp.test.StoreIbesIdentification_jsp._jspService(StoreIbesIdentification_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
Mar 1, 2007 10:58:30 AM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
Mar 1, 2007 10:58:30 AM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
Mar 1, 2007 10:58:30 AM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recomm
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Mar 1, 2007 10:58:30 AM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
Mar 1, 2007 10:58:30 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
Mar 1, 2007 10:58:30 AM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Mar 1, 2007 10:58:30 AM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
Mar 1, 2007 10:58:30 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: 08001
Mar 1, 2007 10:58:30 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: No suitable driver found for jdbc:hsqldb:hsql://localhost
I do notice from the above logs that the code is trying to look for HSQL DB driver instead of MySQL driver. I don't know why it's still looking for HSQL DB when I've specified MySQL in the hibernate db config xml file.
It's probably reading HSQL DB from the build folder which might have old settings.
I'm still looking into this.... at this time.
Any help is appreciated.