-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Lazy collection throw org.hibernate.exception.JDBCConnection
PostPosted: Mon Apr 02, 2012 7:06 am 
Newbie

Joined: Mon Feb 27, 2012 10:22 am
Posts: 3
Hi
I am using Hibernate + Spring.

I have my entity pojos defined with a few one-many and many-one bidirectional mapping. All the objects are associated LAZY. In multithreaded environment, and when my db is too high (since I am using LAZY), I am getting an exception when i try to access any of the instance of an entity pojo or list of entity pojos from the parent entity pojo. This never happens when I am using a single threaded running or when i do very minimal operations.


javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:255)
at com.test.t.platform.core.persistence.PersistenceManagerImpl$QueryManagerImpl.query(PersistenceManagerImpl.java:640)
at com.test.t.services.design.StressThread.getAllCores(StressThread.java:112)
... 1 more
Caused by: org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1700)
at org.hibernate.loader.Loader.doQuery(Loader.java:801)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:479)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:279)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
... 3 more
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:150)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:128)
at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:68)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 25 more
Caused by: java.net.BindException: Address already in use: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at org.postgresql.core.PGStream.<init>(PGStream.java:62)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76)
... 41 more


Top
 Profile  
 
 Post subject: Re: Lazy collection throw org.hibernate.exception.JDBCConnection
PostPosted: Mon Apr 02, 2012 10:34 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Caused by: java.net.BindException: Address already in use: connect

Your jdbc-driver attempts to establish a connection with the database and is'nt able to do it.

How many open connections do you have, when the exception arises?
Do you use a connection-pool implementation where you can check this?


Top
 Profile  
 
 Post subject: Re: Lazy collection throw org.hibernate.exception.JDBCConnection
PostPosted: Mon Apr 02, 2012 11:37 am 
Newbie

Joined: Mon Feb 27, 2012 10:22 am
Posts: 3
Thanks for responding,

Not sure on "How many open connections do you have". How do I check that?

the data source details that i am using in my applicationContext.xml

<bean id="persistence" class="com.t.my.platform.core.persistence.PersistenceManagerImpl" scope="prototype" >
<constructor-arg><ref bean="entityManagerFactory"/></constructor-arg>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="prototype">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${my.database.connection.url}"/>
<property name="username" value="${my.database.connection.username}" />
<property name="password" value="${my.database.connection.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="pu1"/>
<property name="dataSource" ref="myDataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="showSql" value="false" />
<property name="generateDdl" value="true" />
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
<prop key="hibernate.connection.password">${my.database.connection.password}</prop>
<prop key="hibernate.connection.url">${my.database.connection.url}</prop>
<prop key="hibernate.connection.username">${my.database.connection.username}</prop>
</props>
</property>
</bean>


Top
 Profile  
 
 Post subject: Re: Lazy collection throw org.hibernate.exception.JDBCConnection
PostPosted: Tue Apr 03, 2012 5:30 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
You are using org.springframework.jdbc.datasource.DriverManagerDataSource
which according documentation is not an actual connection pool
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/datasource/DriverManagerDataSource.html.

I don't know this class, maybe it gives information about established connections by enabling a higher log-level.
Otherwise I suggest you to use another Datasource with connection pooling,
most of it's modern implementations provide some mechanism to monitor the amount of used connections
(typically over JMX).
Maybe you can also check the number of established connections out from your postgres database?

Last but not least, you as application developer should have control about the number concurrent sessions you use(once you begin a transaction the connection get wired to the session and cannot be shared to others sessions anymore).
Maybe you forget to close sessions in your application, which you don't use anymore?


Top
 Profile  
 
 Post subject: Re: Lazy collection throw org.hibernate.exception.JDBCConnection
PostPosted: Wed Apr 04, 2012 7:37 am 
Newbie

Joined: Mon Feb 27, 2012 10:22 am
Posts: 3
I tried with com.mchange.v2.c3p0.ComboPooledDataSource

Not getting any exception now, but on a rigorous lazy fetches from my internal children object from the parent entity pojo at some points the method just hangs. No error or no exceptions. It just seems to wait indefinitely.

I mean after doing a lot of get..() access, i encounter cases where my application keeps waiting on some get..() methods which are bound lazily.

A question in this context that I am confused about is : I use an entity manager instance to fetch any parent entity pojo instance from the database using some named query. Immedietly after fetching it, i close the entitymanager. Now after this if i make a get...() call on this fetched entity, as per lazy fetching a db query would go and hit the database and get the value for that attribute. Since I have already closed my entitymanager instance, how does my hibernate talk with the database when i call this get...() method. Does hibernate create an entitymanager instance on its own, or is it that no entitymanager instances are required for such lazy call made by hibernate.

My entity manager bean is defined as below.
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">


Top
 Profile  
 
 Post subject: Re: Lazy collection throw org.hibernate.exception.JDBCConnection
PostPosted: Wed Apr 04, 2012 10:30 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Not getting any exception now, but on a rigorous lazy fetches from my internal children object from the parent entity pojo at some points the method just hangs. No error or no exceptions. It just seems to wait indefinitely.


In such case it helps to use
Code:
jstack <jvm-pid>

It shows the stacktrace of all running threads, so we could see where exactly the thread is hanging (or cycling in a endless-loop).

Quote:
Since I have already closed my entitymanager instance, how does my hibernate talk with the database when i call this get...() method.


Usually in this case you should get a LazyInitializationException.
Can you post your code?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.