Announcement Announcement Module
Collapse
No announcement yet.
Connection cannot be null when 'hibernate.dialect' not set Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connection cannot be null when 'hibernate.dialect' not set

    Hi, i am having more issues with seting up hibernate4 with spring3. this time it is saying that connection is nul as the dialect is not set which it is on my hibernate.cfg.xml file.

    here is the full exception:

    Code:
         org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in URL [file:war/WEB-INF/datasource-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
        	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96)
        	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
        	at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
        	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
        	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
        	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
        	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
        	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
        	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
        	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
        	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
        	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
        	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
        	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
        	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
        Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
        	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
        	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
        	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:172)
        	at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
        	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
        	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
        	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
        	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270)
        	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266)
        	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735)
        	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
        	at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
        	at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
        	... 29 more


    Here is my dataSource-config.xml thats ets up the sessionfactory

    Code:
     <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
        	xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
              ">
        
        
        	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        		destroy-method="close">
        		<property name="driverClassName" value="${database.driver}" />
        		<property name="url" value="${database.url}" />
        		<property name="username" value="${database.user}" />
        		<property name="password" value="${database.password} " />
        	</bean>
        
        	<bean id="mySessionFactory"
        		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        		<property name="dataSource" ref="myDataSource" />
        		<property name="packagesToScan" value="com.jr.freedom"/>
        		<property name="hibernateProperties" value="classpath:hibernate.cfg.xml"/>
        					
        	</bean>
        
        	<!-- Declare a transaction manager -->
        	<bean id="transactionManager"
        		class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        		p:sessionFactory-ref="mySessionFactory" />
        
        </beans>

    And below is the hibernate.cfg.xml file



    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>
                <!-- JDBC connection settings -->
                <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="connection.url">jdbc:mysql://localhost:3306/freedom</property>
                <property name="connection.username">root</property>
                <property name="connection.password">password</property>
                 
                <!-- JDBC connection pool, use Hibernate internal connection pool -->
                <property name="connection.pool_size">25</property>
         
                <!-- Defines the SQL dialect used in Hiberante's application -->
                <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>
         
                <!-- Display and format all executed SQL to stdout -->
                <property name="show_sql">true</property>
                <property name="format_sql">true</property>
         
                <!-- Drop and re-create the database schema on startup -->
                <property name="hbm2ddl.auto">create</property>
                 
                <!-- Mapping to hibernate mapping files -->
                <!--mapping resource="org/kodejava/example/hibernate/app/Label.hbm.xml"/-->
            </session-factory>
        </hibernate-configuration>
    As you can see, the dialect is being set.


    edit:
    Code:
    my database.properties file
    
            # DB properties file
        
        database.url=jdbc:mysql://localhost:3306/freedom
        database.driver=com.mysql.jdbc.Driver
        database.user=root
        database.password=password
        database.maxConnections=25

  • #2
    Hello

    Read this 3.4.1. SQL Dialects (Hibernate 4.0)
    Read this 3.4.1. SQL Dialects (Hibernate 4.1)

    Seems must be

    Code:
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    BTW if you are using MySQL 5 you can use

    Code:
    org.hibernate.dialect.MySQL5Dialect
    or
    org.hibernate.dialect.MySQL5InnoDBDialect
    Where you are reading the database.properties file?

    Suggestion: org.apache.commons.dbcp.BasicDataSource is not good for production. Think in use C3PO for example
    Last edited by dr_pompeii; Jun 27th, 2012, 06:10 PM.

    Comment


    • #3
      Originally posted by dr_pompeii View Post
      Hello

      <snip>

      Suggestion: org.apache.commons.dbcp.BasicDataSource is not good for production. Think in use C3PO for example

      Thanks i will have a read and try that.

      Also i have managed to get log4j to work so i can see full stack trace. here is a snippet of it below:

      Code:
      1785 [main] 
      1787 [main] DEBUG org.hibernate.cfg.Configuration  - Resolving reference to class: com.jr.freedom.user.User
      1787 [main] DEBUG org.hibernate.cfg.Configuration  - Resolving reference to class: com.jr.freedom.discusion.Discussion
      1787 [main] DEBUG org.hibernate.cfg.Configuration  - Resolving reference to class: com.jr.freedom.user.User
      1787 [main] DEBUG org.hibernate.cfg.Configuration  - Resolving reference to class: com.jr.freedom.user.ProfileImage
      1791 [main] WARN  org.hibernate.engine.jdbc.internal.JdbcServicesImpl  - HHH000342: Could not obtain connection to query metadata : Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))
      1791 [main] INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory  - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@74c12978: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.config.viewControllerHandlerMapping,userService,myDataSource,mySessionFactory,transactionManager,propertyConfigurer,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,viewResolver,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,hello,userController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
      1791 [main] DEBUG org.springframework.beans.factory.support.DisposableBeanAdapter  - Invoking destroy method 'close' on bean with name 'myDataSource'
      1791 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Retrieved dependent beans for bean '(inner bean)#14': [org.springframework.web.servlet.config.viewControllerHandlerMapping]
      1792 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Retrieved dependent beans for bean '(inner bean)#8': [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0]
      1792 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Retrieved dependent beans for bean '(inner bean)#1': [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0]
      1792 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory  - Retrieved dependent beans for bean '(inner bean)': [org.springframework.web.servlet.handler.MappedInterceptor#0]
      1792 [main] ERROR org.springframework.test.context.TestContextManager  - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3882e4f3] to prepare test instance [com.jr.freedom.controllers.UserControllerTest@31d1fc02]
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in URL [file:war/WEB-INF/datasource-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
      	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
      	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
      	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96)
      	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
      	at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
      	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
      	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
      	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
      	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
      	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
      	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
      	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
      	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
      	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
      	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
      	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
      	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
      	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:172)
      	at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
      	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
      	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
      	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
      	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270)
      	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266)
      	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735)
      	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
      	at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
      	at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
      	... 29 more
      Let me know if u need the full stack trace


      The bit that raised a few eyebrows is this:

      Code:
      Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))
      i am reading database.properties from src folder. i originally had the database.properties inside the web-inf folder. which datasource type should i use? and why is the basic no good for production?

      Comment


      • #4
        HEllo


        Code:
                    <!-- JDBC connection settings -->
                    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
                    <property name="connection.url">jdbc:mysql://localhost:3306/freedom</property>
                    <property name="connection.username">root</property>
                    <property name="connection.password">password</property>
                     
                    <!-- JDBC connection pool, use Hibernate internal connection pool -->
                    <property name="connection.pool_size">25</property>
        I dont remember if the pattern connection. is mandatory. I would say is without that

        I mean, just

        Code:
         <property name="driver_class">com.mysql.jdbc.Driver</property>
        and etc

        Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))
        Be sure your root/password be correct, and try without connection.


        Code:
                    <property name="username">root</property>
                    <property name="password">password</property>
        i am reading database.properties from src folder. i originally had the database.properties inside the web-inf folder.
        Wrong, you must read the database.properties from the classpath located with your WEB-INF directory

        which datasource type should i use? and why is the basic no good for production?
        Use one based with C3PO, the other is basic, has no support for Pool.

        Connection for a Database is expensive, C3PO has a good support.


        HTH

        Comment


        • #5
          As per the specification all the properties should have hibernate. appended. for example.

          Code:
          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
          Last edited by abhinavkansal; Jul 1st, 2012, 09:49 AM.

          Comment

          Working...
          X