I'm running this as a JUnit test.
For some reason Hibernate doesn't get information from the
hibernate.properties file or
the hibernate.cfg.xml file. In this I'm only using the properties file, but I see the same behavior with the xml file.
When I set the properties in the program it works fine, yet it appears to be reading the properties file because the log shows the following line
Code:
12:04:10,431 INFO Environment:494 - loaded properties from resource hibernate.properties: {hibernate.c3p0.timeout=300, hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.c3p0.max_statements=30, hibernate.max_fetch_depth=1, hibernate.c3p0.max_size=20, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.c3p0.idle_test_period=3001, hibernate.jdbc.use_streams_for_binary=true, hibernate.c3p0.min_size=5, hibernate.jdbc.batch_size=0, hibernate.connection.username=hiberuser, hibernate.connection.url=jdbc:mysql:///helloworld, hibernate.show_sql=true, hibernate.connection.password=****}
You will notice that the idle_test_period is 3001 not 3000 yet 3000 is what is in my code, so these values are coming from my hibernate.properties file. Yet when I don't do the configuration in the code, it doesn't work. It tells me that I have to specify a dialect, or that I don't have a datasource.
hibernate.propertiesCode:
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
## MySQL
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///helloworld
hibernate.connection.username hiberuser
hibernate.connection.password secret
###########################
### C3P0 Connection Pool###
###########################
hibernate.c3p0.min_size 5
hibernate.c3p0.max_size 20
hibernate.c3p0.timeout 300
hibernate.c3p0.max_statements 30
hibernate.c3p0.idle_test_period 3001
##############################
### Miscellaneous Settings ###
##############################
## print all generated SQL to the console
hibernate.show_sql true
Hibernate version:3.1 intialization code:When the code in beginning "hibernate.dialect" through "hibernate:show sql" is removed it complains about not setting the dialect.
Code:
Configuration cfg = new Configuration();
cfg.addResource("hello/Message.hbm.xml");
cfg.setProperties( System.getProperties() );
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
cfg.setProperty("hibernate.connection.url", "jdbc:mysql:///helloworld");
cfg.setProperty("hibernate.connection.username", "hiberuser");
cfg.setProperty("hibernate.connection.password", "secret");
cfg.setProperty("hibernate.c3p0.min_size", "5");
cfg.setProperty("hibernate.c3p0.max_size", "20");
cfg.setProperty("hibernate.c3p0.timeout", "300");
cfg.setProperty("hibernate.c3p0.max_statements", "30");
cfg.setProperty("hibernate.c3p0.idle_test_period", "3000");
cfg.setProperty("hibernate.show_sql", "true");
SessionFactory sessions = cfg.buildSessionFactory();
Full stack trace of any exception that occurs:Quote:
org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:378)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:110)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1174)
at hello.Message.getSessionFactory(Message.java:110)
at hello.Message.initMessage(Message.java:64)
at hello.MessageTest.testInit(MessageTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using:MySQL (4.0.20), database named helloworld,
Log file without configuration code in the programCode:
12:55:22,610 INFO Environment:479 - Hibernate 3.1.2
12:55:22,806 INFO Environment:494 - loaded properties from resource hibernate.properties: {hibernate.c3p0.timeout=300, hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.c3p0.max_statements=30, hibernate.max_fetch_depth=1, hibernate.c3p0.max_size=20, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.c3p0.idle_test_period=3001, hibernate.jdbc.use_streams_for_binary=true, hibernate.c3p0.min_size=5, hibernate.jdbc.batch_size=0, hibernate.connection.username=hiberuser, hibernate.connection.url=jdbc:mysql:///helloworld, hibernate.show_sql=true, hibernate.connection.password=****}
12:55:22,824 INFO Environment:524 - using java.io streams to persist binary types
12:55:22,862 INFO Environment:525 - using CGLIB reflection optimizer
12:55:22,877 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
12:55:23,193 INFO Configuration:469 - Reading mappings from resource: hello/Message.hbm.xml
12:55:23,382 DEBUG DTDEntityResolver:42 - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd in classpath under org/hibernate/
12:55:23,395 DEBUG DTDEntityResolver:56 - found http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd in classpath
12:55:23,814 INFO HbmBinder:309 - Mapping class: hello.Message -> MESSAGES
12:55:23,892 DEBUG HbmBinder:1261 - Mapped property: id -> MESSAGE_ID
12:55:23,952 DEBUG HbmBinder:1261 - Mapped property: text -> MESSAGE_TEXT
12:55:24,199 DEBUG HbmBinder:1261 - Mapped property: nextMessage -> NEXT_MESSAGE_ID
12:55:24,231 DEBUG Configuration:1167 - Preparing to build session factory with filters : {}
12:55:24,241 DEBUG Configuration:1002 - processing extends queue
12:55:24,261 DEBUG Configuration:1006 - processing collection mappings
12:55:24,263 DEBUG Configuration:1017 - processing native query and ResultSetMapping mappings
12:55:24,266 DEBUG Configuration:1025 - processing association property references
12:55:24,268 DEBUG Configuration:1047 - processing foreign key constraints
12:55:24,300 DEBUG Configuration:1130 - resolving reference to class: hello.Message
12:55:24,355 WARN UserSuppliedConnectionProvider:23 - No connection properties specified - the user must supply JDBC connections
Log file with configuration code in the programCode:
12:04:10,251 INFO Environment:479 - Hibernate 3.1.2
12:04:10,431 INFO Environment:494 - loaded properties from resource hibernate.properties: {hibernate.c3p0.timeout=300, hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.c3p0.max_statements=30, hibernate.max_fetch_depth=1, hibernate.c3p0.max_size=20, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.c3p0.idle_test_period=3001, hibernate.jdbc.use_streams_for_binary=true, hibernate.c3p0.min_size=5, hibernate.jdbc.batch_size=0, hibernate.connection.username=hiberuser, hibernate.connection.url=jdbc:mysql:///helloworld, hibernate.show_sql=true, hibernate.connection.password=****}
12:04:10,455 INFO Environment:524 - using java.io streams to persist binary types
12:04:10,523 INFO Environment:525 - using CGLIB reflection optimizer
12:04:10,531 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
12:04:11,078 INFO Configuration:469 - Reading mappings from resource: hello/Message.hbm.xml
12:04:11,272 DEBUG DTDEntityResolver:42 - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd in classpath under org/hibernate/
12:04:11,288 DEBUG DTDEntityResolver:56 - found http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd in classpath
12:04:11,694 INFO HbmBinder:309 - Mapping class: hello.Message -> MESSAGES
12:04:11,770 DEBUG HbmBinder:1261 - Mapped property: id -> MESSAGE_ID
12:04:11,831 DEBUG HbmBinder:1261 - Mapped property: text -> MESSAGE_TEXT
12:04:12,077 DEBUG HbmBinder:1261 - Mapped property: nextMessage -> NEXT_MESSAGE_ID
12:04:12,092 DEBUG Configuration:1167 - Preparing to build session factory with filters : {}
12:04:12,103 DEBUG Configuration:1002 - processing extends queue
12:04:12,114 DEBUG Configuration:1006 - processing collection mappings
12:04:12,127 DEBUG Configuration:1017 - processing native query and ResultSetMapping mappings
12:04:12,159 DEBUG Configuration:1025 - processing association property references
12:04:12,172 DEBUG Configuration:1047 - processing foreign key constraints
12:04:12,200 DEBUG Configuration:1130 - resolving reference to class: hello.Message
12:04:12,234 INFO C3P0ConnectionProvider:50 - C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql:///helloworld
12:04:12,273 INFO C3P0ConnectionProvider:51 - Connection properties: {user=hiberuser, password=****}
12:04:12,283 INFO C3P0ConnectionProvider:54 - autocommit mode: false
12:04:12,527 INFO MLog:80 - MLog clients using log4j logging.
12:04:12,665 INFO C3P0Registry:77 - Initializing c3p0-0.9.0 [built 11-July-2005 00:43:29 -0400; debug? true; trace: 10]
12:04:13,410 INFO PoolBackedDataSource:257 - Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@6bd530 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@593ce6 [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 593ce6, idleConnectionTestPeriod -> 3000, initialPoolSize -> 5, maxIdleTime -> 300, maxPoolSize -> 20, maxStatements -> 30, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@4f7a86 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 4f7a86, jdbcUrl -> jdbc:mysql:///helloworld, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ], factoryClassLocation -> null, identityToken -> 6bd530, numHelperThreads -> 3 ]
12:04:13,565 DEBUG BasicResourcePool:942 - awaitAvailable(): [unknown]
12:04:13,584 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 0, unused: 0, excluded: 0]
12:04:13,763 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 1, unused: 1, excluded: 0]
12:04:13,775 DEBUG BasicResourcePool:1081 - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@adb3f ---> age: 12 max: 300000 [com.mchange.v2.resourcepool.BasicResourcePool@e6235d]
12:04:13,820 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 1, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@adb3f)
12:04:13,907 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 2, unused: 1, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@adb3f)
12:04:13,928 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@adb3f)
12:04:14,106 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 4, unused: 3, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@adb3f)
12:04:14,114 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@adb3f)
12:04:14,160 DEBUG SettingsFactory:299 - could not get database version from JDBC metadata
12:04:14,174 INFO SettingsFactory:77 - RDBMS: MySQL, version: 4.0.20-standard-log
12:04:14,186 INFO SettingsFactory:78 - JDBC driver: Mark Matthews' MySQL Driver, version: 2.0.14
12:04:14,197 DEBUG GooGooStatementCache:197 - checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 0; checked out: 0; num connections: 0; num keys: 0
12:04:14,417 DEBUG NewPooledConnection:294 - A Throwable occurred while trying to reset the typeMap property of our Connection to Collections.EMPTY_MAP!
com.mysql.jdbc.jdbc2.NotImplemented
at com.mysql.jdbc.jdbc2.Connection.setTypeMap(Unknown Source)
at com.mchange.v2.c3p0.impl.NewPooledConnection.reset(NewPooledConnection.java:290)
at com.mchange.v2.c3p0.impl.NewPooledConnection.markClosedProxyConnection(NewPooledConnection.java:247)
at com.mchange.v2.c3p0.impl.NewProxyConnection.close(NewProxyConnection.java:1148)
at org.hibernate.connection.C3P0ConnectionProvider.closeConnection(C3P0ConnectionProvider.java:42)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:99)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1174)
at hello.Message.getSessionFactory(Message.java:110)
at hello.Message.initMessage(Message.java:64)
at hello.MessageTest.testInit(MessageTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
12:04:14,436 DEBUG BasicResourcePool:1159 - trace com.mchange.v2.resourcepool.BasicResourcePool@e6235d [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@adb3f)
12:04:14,508 INFO Dialect:103 - Using dialect: org.hibernate.dialect.MySQLDialect