Hi guys, I am running into this weird jdbc problem that can really use some help to spot out what is wrong with my setup.
I have a jsf2 webapp running inside tomcat7, using hibernate with mysql 5.1 on a ubuntu 10.04.4 LTS. Everything works fine on my development machine (with the exact same setup). But at my server, accessing any jsf page the first time will result "No suitable driver found for jdbc" exception whenever tomcat had reboot or if the old connection had expired/timeout. In order for me to get it running again, I created a dummy test.jsp page with the following snippet of code:
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
Connection con = null;
try
{
String connectionURL = "jdbc:mysql://localhost/myWebApp";
String user = "user";
String password = "password";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL,user,password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select version()");
if(rs.next())
out.println("version is: " + rs.getString(1));
else
out.println("nothing!");
}
catch(SQLException ex)
{
throw new ServletException("Servlet could not display records.",ex);
}
catch(ClassNotFoundException ex)
{
throw new ServletException("JDBC Driver not found",ex);
}
%>
after I ran the page above, my jsf app works again, until the session expire out of a long idle. I have tried move the mysql-connector-java-5.1.21-bin.jar from the %tomcat_home%/webapps/myWebApp/WEB-INF/lib to %tomcat_home%/lib, no luck. same thing still happens.
Wonder if anyone can share some thoughts on it... here's my hibernate config:
<!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>
<property name="connection.url">jdbc:mysql://localhost/myWebApp</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.myApp.test.MyClass"/>
</session-factory>
</hibernate-configuration>
here's the error message when accessing a jsf page right after rebooting tomcat:
org.hibernate.exception.JDBCConnectionException: Could not open connection
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1363)
java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1/myWebApp java.sql.DriverManager.getConnection(DriverManager.java:640)
java.sql.DriverManager.getConnection(DriverManager.java:169)
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1363)
I've turned on the hibernate log at log4j, this is what it says when it tries to access database right after tomcat got rebooted:
22:05:39,615 INFO Version:37 - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
22:05:39,644 INFO Version:41 - HHH000412: Hibernate Core {4.1.2.Final}
22:05:39,659 INFO Environment:239 - HHH000206: hibernate.properties not found
22:05:39,663 INFO Environment:342 - HHH000021: Bytecode provider name : javassist
22:05:39,764 INFO Configuration:1924 - HHH000043: Configuring from resource: /hibernate.cfg.xml
22:05:39,765 INFO Configuration:1943 - HHH000040: Configuration resource: /hibernate.cfg.xml
22:05:39,958 INFO Configuration:2065 - HHH000041: Configured SessionFactory: null
22:05:40,506 INFO DriverManagerConnectionProviderImpl:96 - HHH000402: Using Hibernate built-in connection pool (not for production use!)
22:05:40,510 INFO DriverManagerConnectionProviderImpl:129 - HHH000115: Hibernate connection pool size: 20
22:05:40,511 INFO DriverManagerConnectionProviderImpl:132 - HHH000006: Autocommit mode: false
22:05:40,512 INFO DriverManagerConnectionProviderImpl:146 - HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1/myWebApp]
22:05:40,513 INFO DriverManagerConnectionProviderImpl:149 - HHH000046: Connection properties: {user=user, password=****}
22:05:40,558 WARN JdbcServicesImpl:169 - HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:mysql://127.0.0.1/myWebApp
22:05:40,573 INFO Dialect:122 - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
22:05:40,598 INFO LobCreatorBuilder:85 - HHH000422: Disabling contextual LOB creation as connection was null
22:05:40,633 INFO TransactionFactoryInitiator:73 - HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
22:05:40,659 INFO ASTQueryTranslatorFactory:48 - HHH000397: Using ASTQueryTranslatorFactory
22:05:41,595 WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08001
22:05:41,596 ERROR SqlExceptionHelper:144 - No suitable driver found for jdbc:mysql://127.0.0.1/myWebApp
This is pretty silly, if I have to call test.jsp everytime before I use the app, and since this works on my development box, obviously has something to do with config setting!?
maybe at worst case, I will just insert that jdbc code in a filter and force it to call db before requesting any page!? but that doesn't make sense from an implementation perspective... :(
Thank you for your time!