File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes org.hibernate.exception.JDBCConnectionException: Cannot open connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "org.hibernate.exception.JDBCConnectionException: Cannot open connection" Watch "org.hibernate.exception.JDBCConnectionException: Cannot open connection" New topic

org.hibernate.exception.JDBCConnectionException: Cannot open connection

Mario Schwaiger

Joined: Jun 14, 2010
Posts: 16

I am using JSP and Hibernate (Pooling with BoneCP to a MSSQL-Server 2012) for a web-project. Sometimes it occurs I get the exception "org.hibernate.exception.JDBCConnectionException: Cannot open connection" and think it might be a timeout. I guess I'm overseeing something.
About five minutes ago I tried it, everything worked fine. In the moment I run everything again, it works again (So the database-connection is set up right!). I am a bit puzzled... As I remember I've closed all my sessions... Here's my code:

In some books I found I should commit a session.close() in the end, but as I do that an exception is thrown, the connection is closed already. I googled this issue and found here: a comment by Mr. Durchholz, I'm not sure if I get that in the correct conext:
That is a configuration setting. By default, Hibernate closes a session on commit.

My hibernate.cfg.xml:

And the thrown exception...

INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
[main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.10.Final
[main] INFO org.hibernate.cfg.Environment - not found
[main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
[main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
[main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
[main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
[main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace Use namespace instead. Refer to Hibernate 3.6 Migration Guide!
[main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
[main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: project.Orders
[main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity project.Orders on table [Project].[dbo].[Orders]
[main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class:project.Orderlines
[main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity project.Orderlines on table [Project].[dbo].[Orderlines]
[main] INFO org.hibernate.cfg.annotations.CollectionBinder - Mapping collection: project.Orders.orderlines -> [Project].[dbo].[Orderlines]
[main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
[main] INFO - Unable to find on the classpath. Hibernate Search is not enabled.
[main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
[main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
[main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
[main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://localhost:1433
[main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=mario, password=****}
[main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.SQLServerDialect
[main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as connection was null
[main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
[main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
[main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
[main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
[main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
[main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
[main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
[main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
[main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
[main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
[main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
[main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
[main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@155b88b
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@c6b797
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : org.hibernate.type.ClobType@3f4c24
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@3f4c24
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : org.hibernate.type.BlobType@19fdae5
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@19fdae5
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@1dbfb7a
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@15b2028
[main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@19c5407
[main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
[main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 08S01
[main] ERROR org.hibernate.util.JDBCExceptionReporter - I/O Error: Connection reset
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(
at org.hibernate.exception.JDBCExceptionHelper.convert(
at org.hibernate.exception.JDBCExceptionHelper.convert(
at org.hibernate.jdbc.ConnectionManager.openConnection(
at org.hibernate.jdbc.ConnectionManager.getConnection(
at org.hibernate.jdbc.JDBCContext.connection(
at org.hibernate.transaction.JDBCTransaction.begin(
at org.hibernate.impl.SessionImpl.beginTransaction(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(
at com.sun.proxy.$Proxy11.beginTransaction(Unknown Source)
at project.main.main(
Caused by: java.sql.SQLException: I/O Error: Connection reset
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(
at net.sourceforge.jtds.jdbc.TdsCore.login(
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(
at net.sourceforge.jtds.jdbc.Driver.connect(
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(
at org.hibernate.jdbc.ConnectionManager.openConnection(
... 12 more
Caused by: Connection reset
at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(
at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(
at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(
... 19 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 45 seconds)

Any ideas? Thank you!
Mario Schwaiger

Joined: Jun 14, 2010
Posts: 16
I agree. Here's the link:
subject: org.hibernate.exception.JDBCConnectionException: Cannot open connection
It's not a secret anymore!