-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate with two connections
PostPosted: Tue Jan 18, 2005 9:55 am 
Beginner
Beginner

Joined: Thu Jan 01, 2004 11:36 am
Posts: 23
Location: Belgium
Hibernate version: 2.1.2
Name and version of the database you are using: oracle 9i

Hello,

I am trying to setup an application in a weblogic 8.1 server using two connections to Oracle 9i (two different schemas)

the datasources and pools are configured in weblogic using the following names:
NDPdocDatasource using pool NDPdocpool
NDPscanDatasource using pool NDPscanpool

I have configured two separate hibernate.cfg.xml files (listed below) in a path /config/scanbase and /config/docbase, and the factories are called as listed below.

The purpose is to have the two databases available; however, when I call the following code, I get an error for the connection NDPdocDatasource which is called in the second part of the code. Apparently weblogic tries to create a connection in the wrong pool (java.sql.SQLException: Connection has already been created in this tx context for pool named NDPscanpool. Illegal attempt to create connection from another pool: NDPdocpool);

Is my hibernate configuration OK ? I also get a warning "WARN SettingsFactory:95 - Could not obtain connection metadata".

some hints would be welcome.

Jan


Code between sessionFactory.openSession() and session.close():


public void testConnections() throws DatasourceException {
Session session1 = null;
Session session2 = null;

try {
try {
// connection to first db
Person person = null;
session1 = Scanbase.getSession();
session1.beginTransaction();
person = (Person) session1.load(Person.class, new Long(1));
} catch (HibernateException e) {
throw new DatasourceException(e);
}

try {
//connection to second db fails
Testdoc testdoc = null;
session2 = Docbase.getSession();
session2.beginTransaction();
testdoc = (Testdoc) session2.load(Testdoc.class, new Long(1));
} catch (HibernateException e) {
throw new DatasourceException(e.getMessage());
}
} finally {
Scanbase.closeSession();
Docbase.closeSession();
}

}




public class Docbase {
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();

static {
try {
AppLogger.debug("initializing hib. sessionfactory for DOC database");
new Configuration().configure("/config/docbase/hibernate.cfg.xml").buildSessionFactory();

} catch (HibernateException ex) {
AppLogger.error("exception during initialization of hibernate sessionFactory for DOC database: " + ex.getMessage());
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage() );
}
}

...

public class Scanbase {

private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();

static {
try {
AppLogger.debug("initializing hib. sessionfactory for scan database");
new Configuration().configure("/config/scanbase/hibernate.cfg.xml").buildSessionFactory();

} catch (HibernateException ex) {
AppLogger.error("exception during initialization of hibernate sessionFactory for scan database: " + ex.getMessage());
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage() );
}
}


Mapping documents:

------- /config/docbase/hibernate.cfg.xml
...

<hibernate-configuration>

<session-factory name="hibernate.HibernateFactoryDoc">
<!-- Bind the SesssionFactory to JNDI -->

<!-- JNDI DataSource Connection -->
<property name="connection.datasource">NDPdocDatasource</property>

<!-- Transaction Management Properties-->
<property name="transaction.manager_lookup_class">
net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
</property>

<property name="transaction.factory_class">
net.sf.hibernate.transaction.JTATransactionFactory
</property>

<property name="session_factory_name">DocSessionFactory</property>
<!-- Miscellaneous Properties -->
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="use_outer_join ">true</property>
<property name="max_fetch_depth">2</property>

<!-- mapping files -->
<mapping resource="config/commonMappings/systemUser.hbm.xml"/>
<mapping resource="config/commonMappings/role.hbm.xml"/>
<mapping resource="config/docbase/testdoc.hbm.xml"/>

</session-factory>


</hibernate-configuration>

--------- config/scanbase/hibernate.cfg.xml

<hibernate-configuration>

<session-factory name="hibernate.HibernateFactoryScan">

<!-- JNDI DataSource Connection -->
<property name="connection.datasource">NDPscanDatasource</property>

<!-- Transaction Management Properties-->
<property name="transaction.manager_lookup_class">
net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
</property>

<property name="transaction.factory_class">
net.sf.hibernate.transaction.JTATransactionFactory
</property>

<property name="session_factory_name">ScanSessionFactory</property>
<!-- Miscellaneous Properties -->
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>

<property name="use_outer_join ">true</property>
<property name="max_fetch_depth">2</property>

<!-- mapping files-->
<mapping resource="config/scanbase/person.hbm.xml"/>
<mapping resource="config/commonMappings/systemUser.hbm.xml"/>
<mapping resource="config/commonMappings/role.hbm.xml"/>
<mapping resource="config/scanbase/batch.hbm.xml"/>
<mapping resource="config/scanbase/scanner.hbm.xml"/>
<mapping resource="config/scanbase/centreGest.hbm.xml"/>
<mapping resource="config/scanbase/scannedDocument.hbm.xml"/>

</session-factory>


</hibernate-configuration>

Full stack trace of any exception that occurs:

14:32:16,302 DEBUG object:39 - initializing hib. sessionfactory for scan database
14:32:16,365 INFO Environment:462 - Hibernate 2.1.2
14:32:16,365 INFO Environment:491 - hibernate.properties not found
14:32:16,365 INFO Environment:519 - using CGLIB reflection optimizer
14:32:16,380 INFO Configuration:854 - configuring from resource: /config/scanbase/hibernate.cfg.xml
14:32:16,380 INFO Configuration:826 - Configuration resource: /config/scanbase/hibernate.cfg.xml
14:32:16,661 INFO Configuration:311 - Mapping resource: config/scanbase/person.hbm.xml
14:32:16,755 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.Person -> NDP_PERSONNES
14:32:17,068 INFO Configuration:311 - Mapping resource: config/commonMappings/systemUser.hbm.xml
14:32:17,099 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.SystemUser -> NDP_UTILISATEURS
14:32:17,130 INFO Configuration:311 - Mapping resource: config/commonMappings/role.hbm.xml
14:32:17,208 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.Role -> NDP_SEC_ROLES
14:32:17,224 INFO Configuration:311 - Mapping resource: config/scanbase/batch.hbm.xml
14:32:17,255 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.Batch -> NDP_LOTS
14:32:17,896 INFO Configuration:311 - Mapping resource: config/scanbase/scanner.hbm.xml
14:32:17,943 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.Scanner -> NDP_REF_SCANNERS
14:32:17,943 INFO Configuration:311 - Mapping resource: config/scanbase/centreGest.hbm.xml
14:32:17,958 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.CentreGest -> NDP_REF_CENTRES_GEST
14:32:17,974 INFO Configuration:311 - Mapping resource: config/scanbase/scannedDocument.hbm.xml
14:32:17,989 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.ScannedDocument -> NDP_DOCUMENTS_SCAN
14:32:18,005 INFO Configuration:1017 - Configured SessionFactory: hibernate.HibernateFactoryScan
14:32:18,005 INFO Configuration:595 - processing one-to-many association mappings
14:32:18,005 INFO Binder:1154 - Mapping collection: eu.cec.admin.ndp.object.domain.Batch.scannedDocuments -> NDP_DOCUMENTS_SCAN
14:32:18,005 INFO Configuration:604 - processing one-to-one association property references
14:32:18,005 INFO Configuration:629 - processing foreign key constraints
14:32:18,396 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.OracleDialect
14:32:18,505 INFO SettingsFactory:58 - Maximim outer join fetch depth: 2
14:32:18,583 INFO SettingsFactory:62 - Use outer join fetching: true
14:32:18,599 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:18,599 INFO DatasourceConnectionProvider:51 - Using datasource: NDPscanDatasource
14:32:18,599 INFO TransactionFactoryFactory:31 - Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
14:32:18,614 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:18,614 INFO TransactionManagerLookupFactory:38 - instantiating TransactionManagerLookup: net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
14:32:18,630 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
14:32:18,630 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:18,692 INFO TransactionManagerLookupFactory:38 - instantiating TransactionManagerLookup: net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
14:32:18,692 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
14:32:18,739 INFO SettingsFactory:102 - Use scrollable result sets: true
14:32:18,739 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): true
14:32:18,739 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
14:32:18,739 INFO SettingsFactory:114 - echoing all SQL to stdout
14:32:18,739 INFO SettingsFactory:117 - Query language substitutions: {}
14:32:18,739 INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
14:32:18,771 INFO Configuration:1080 - instantiating and configuring caches
14:32:19,192 INFO SessionFactoryImpl:119 - building session factory
14:32:20,380 INFO SessionFactoryObjectFactory:86 - Factory name: ScanSessionFactory
14:32:20,536 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:20,583 INFO SessionFactoryObjectFactory:91 - Bound factory to JNDI name: ScanSessionFactory
14:32:20,583 WARN SessionFactoryObjectFactory:101 - InitialContext did not implement EventContext
14:32:20,583 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:20,583 DEBUG object:39 - scanbase: Opening new Session for this thread.
14:32:20,708 DEBUG SQL:237 - select person0_.PER_ID as PER_ID0_, person0_.NO_SYSPER as NO_SYSPER0_, person0_.NOM_NAISS as NOM_NAISS0_, person0_.NOM_USUEL as NOM_USUEL0_, person0_.PRENOM as PRENOM0_, person0_.dt_naiss as dt_naiss0_, person0_.sex_Cd as sex_Cd0_ from NDP_PERSONNES person0_ where person0_.PER_ID=?
14:32:20,739 DEBUG object:39 - initializing hib. sessionfactory for DOC database
14:32:20,739 INFO Configuration:854 - configuring from resource: /config/docbase/hibernate.cfg.xml
14:32:20,739 INFO Configuration:826 - Configuration resource: /config/docbase/hibernate.cfg.xml
14:32:20,755 INFO Configuration:311 - Mapping resource: config/commonMappings/systemUser.hbm.xml
14:32:20,848 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.SystemUser -> NDP_UTILISATEURS
14:32:21,052 INFO Configuration:311 - Mapping resource: config/commonMappings/role.hbm.xml
14:32:21,083 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.Role -> NDP_SEC_ROLES
14:32:21,083 INFO Configuration:311 - Mapping resource: config/docbase/testdoc.hbm.xml
14:32:21,114 INFO Binder:229 - Mapping class: eu.cec.admin.ndp.object.domain.Testdoc -> testdoc
14:32:21,114 INFO Configuration:1017 - Configured SessionFactory: hibernate.HibernateFactoryDoc
14:32:21,114 INFO Configuration:595 - processing one-to-many association mappings
14:32:21,114 INFO Configuration:604 - processing one-to-one association property references
14:32:21,130 INFO Configuration:629 - processing foreign key constraints
14:32:21,145 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.OracleDialect
14:32:21,145 INFO SettingsFactory:58 - Maximim outer join fetch depth: 2
14:32:21,145 INFO SettingsFactory:62 - Use outer join fetching: true
14:32:21,145 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:21,145 INFO DatasourceConnectionProvider:51 - Using datasource: NDPdocDatasource
14:32:21,145 INFO TransactionFactoryFactory:31 - Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
14:32:21,145 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:21,161 INFO TransactionManagerLookupFactory:38 - instantiating TransactionManagerLookup: net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
14:32:21,161 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
14:32:21,161 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:21,161 INFO TransactionManagerLookupFactory:38 - instantiating TransactionManagerLookup: net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
14:32:21,161 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
14:32:21,161 WARN SettingsFactory:95 - Could not obtain connection metadata
java.sql.SQLException: Connection has already been created in this tx context for pool named NDPscanpool. Illegal attempt to create connection from another pool: NDPdocpool
at weblogic.jdbc.jts.Driver.getExistingConnection(Driver.java:473)
at weblogic.jdbc.jts.Driver.connect(Driver.java:142)
at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:305)
at net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:59)
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:72)
at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1119)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:748)
at eu.cec.admin.ndp.object.persistence.Docbase.<clinit>(Docbase.java:34)
at eu.cec.admin.ndp.object.scan.DemoServiceEJB.testConnections(DemoServiceEJB.java:122)
at eu.cec.admin.ndp.object.scan.DemoService_8jx1fn_EOImpl.testConnections(DemoService_8jx1fn_EOImpl.java:100)
at eu.cec.admin.ndp.object.scan.DemoService_8jx1fn_EOImpl_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:477)
at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java:108)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:420)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:144)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:415)
at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:30)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
14:32:21,177 INFO SettingsFactory:102 - Use scrollable result sets: false
14:32:21,192 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
14:32:21,317 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
14:32:21,317 INFO SettingsFactory:114 - echoing all SQL to stdout
14:32:21,317 INFO SettingsFactory:117 - Query language substitutions: {}
14:32:21,317 INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
14:32:21,317 INFO Configuration:1080 - instantiating and configuring caches
14:32:21,317 INFO SessionFactoryImpl:119 - building session factory
14:32:21,442 INFO SessionFactoryObjectFactory:86 - Factory name: DocSessionFactory
14:32:21,442 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:21,442 INFO SessionFactoryObjectFactory:91 - Bound factory to JNDI name: DocSessionFactory
14:32:21,505 WARN SessionFactoryObjectFactory:101 - InitialContext did not implement EventContext
14:32:21,661 INFO NamingHelper:26 - JNDI InitialContext properties:{}
14:32:21,676 DEBUG object:39 - DOCbase: Opening new Session for this thread.
14:32:21,692 WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: null
14:32:21,692 ERROR JDBCExceptionReporter:46 - Connection has already been created in this tx context for pool named NDPscanpool. Illegal attempt to create connection from another pool: NDPdocpool
14:32:21,692 ERROR JDBCExceptionReporter:38 - Cannot open connection
java.sql.SQLException: Connection has already been created in this tx context for pool named NDPscanpool. Illegal attempt to create connection from another pool: NDPdocpool
at weblogic.jdbc.jts.Driver.getExistingConnection(Driver.java:473)
at weblogic.jdbc.jts.Driver.connect(Driver.java:142)
at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:305)
at net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:59)
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:278)
at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3264)
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3244)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:65)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:704)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:185)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:831)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:851)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2081)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1955)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1884)
at eu.cec.admin.ndp.object.scan.DemoServiceEJB.testConnections(DemoServiceEJB.java:124)
at eu.cec.admin.ndp.object.scan.DemoService_8jx1fn_EOImpl.testConnections(DemoService_8jx1fn_EOImpl.java:100)
at eu.cec.admin.ndp.object.scan.DemoService_8jx1fn_EOImpl_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:477)
at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java:108)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:420)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:144)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:415)
at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:30)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
14:32:21,801 DEBUG object:39 - Closing Session of this thread.
14:32:21,817 DEBUG object:39 - Closing Session of this thread.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 10:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Use XA datasources

_________________
Emmanuel


Top
 Profile  
 
 Post subject: XA datasources and 2 connections
PostPosted: Wed Jan 19, 2005 4:20 am 
Beginner
Beginner

Joined: Thu Jan 01, 2004 11:36 am
Posts: 23
Location: Belgium
Emmanuel,

Thanks, it works;

I just changed the drivers of the connection pools to oracle.jdbc.xa.client.OracleXADataSource (labeled "driver classname")

Jan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.