怎样用Hibernate配置多个数据源

怎样用Hibernate配置多个数据源,第1张

如果用xml配置的话,那就写两个配置文件,可以不再用“hibernatecfgxml”做文件名,随便什么都可以,像“mysqlcfgxml”或“sqlserverxml”都行。用Configuration类获取SessionFactory的代码:

SessionFactory mysqlFactory = new Configuration()configure("mysqlcfgxml")buildSessionFactory();

SessionFactory sqlserverFactory = new Configuration()configure("sqlserverxml")buildSessionFactory();

如果你用spring,多数据库就更简单了,像这段代码可以完成所有配置:

<beans>

<bean id="mysqlDS" class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">

<property name="url">

<value>jdbc:mysql://localhost:3306/test</value>

</property>

<property name="driverClassName">

<value>orggjtmmmysqlDriver</value>

</property>

<property name="username">

<value>root</value>

</property>

<property name="password">

<value>123456</value>

</property>

</bean>

<bean id="mysqlFactory" class="orgspringframeworkormhibernate3LocalSessionFactoryBean">

<property name="dataSource">

<ref local="mysqlDS"/>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernatedialect">orghibernatedialectMySQLDialect</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>testhbmxml</value>

</list>

</property>

</bean>

<bean id="sqlserverDS" class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">

<property name="url">

<value>jdbc:odbc:test</value>

</property>

<property name="driverClassName">

<value>sunjdbcodbcJdbcOdbcDriver</value>

</property>

<property name="username">

<value>root</value>

</property>

<property name="password">

<value>123456</value>

</property>

</bean>

<bean id="sqlserverFactory" class="orgspringframeworkormhibernate3LocalSessionFactoryBean">

<property name="dataSource">

<ref local="sqlserverDS"/>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernatedialect">orghibernatedialectSQLServerDialect</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>testhbmxml</value>

</list>

</property>

</bean>

</beans>

hibernate和spring还有很多可行的配置,可以参考他们的references,有很详细地说明的。

javasqlSQLException: Access denied for user 'root'@'localhost' (using password: NO)

是你写的密码错误吧 有可能默认密码不对 或者你安装软件时候添了密码 把默认密码给改了

1 连接到CDB

和普通实例一样的连接。 指定ORACLE_SID 以后可以使用OS认证,也可以使用密码进行连接。

[oracle@Ora12c /]$ echo $ORACLE_SID

cndba

[oracle@Ora12c /]$ sqlplus / as sysdba

SQLPlus: Release 121010 Production onMon Apr 28 11:33:43 2014

Copyright (c) 1982, 2013, Oracle All rights reserved

Connected to:

Oracle Database 12c Enterprise EditionRelease 121010 - 64bit Production

With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options

SQL> conn system/oracle

Connected

SQL>

--查看CDB中可用的service:

SQL> COLUMN name FORMAT A30

SQL> SELECT name,pdb

2 FROM v$services

3 ORDER BY name;

NAME PDB

------------------------------------------------------------

SYS$BACKGROUND CDB$ROOT

SYS$USERS CDB$ROOT

cndba CDB$ROOT

cndbaXDB CDB$ROOT

pcndba2 PCNDBA2

pdbcndba PDBCNDBA

6 rows selected

--通过lsnrctl 也可以判断:

[oracle@Ora12c /]$ lsnrctl service

LSNRCTL for Linux: Version 121010 -Production on 28-APR-2014 11:35:31

Copyright (c) 1991, 2013, Oracle All rights reserved

Connecting to(DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))

Services Summary

Service "cndba" has 1instance(s)

Instance "cndba", status READY, has 1 handler(s) for thisservice

Handler(s):

"DEDICATED" established:0 refused:0 state:ready

LOCAL SERVER

Service "cndbaXDB" has 1instance(s)

Instance "cndba", status READY, has 1 handler(s) for thisservice

Handler(s):

"D000" established:0 refused:0 current:0 max:1022 state:ready

DISPATCHER <machine: Ora12c, pid: 10085>

(ADDRESS=(PROTOCOL=tcp)(HOST=Ora12c)(PORT=14696))

Service "pcndba2" has 1instance(s)

Instance "cndba", status READY, has 1 handler(s) for thisservice

Handler(s):

"DEDICATED" established:0 refused:0 state:ready

LOCAL SERVER

Service "pdbcndba" has 1instance(s)

Instance "cndba", status READY, has 1 handler(s) for thisservice

Handler(s):

"DEDICATED" established:0 refused:0 state:ready

LOCAL SERVER

The command completed successfully

[oracle@Ora12c /]$

通过这些service,就可以远程连接CDB。

--EZCONNECT

C:\Users\Dave>sqlplussystem/oracle@192168110:1521/cndba

SQLPlus: Release 112010 Production on 星期三 4月 30 11:36:48 2014

Copyright (c) 1982, 2010, Oracle All rights reserved

连接到:

Oracle Database 12c Enterprise EditionRelease 121010 - 64bit Production

With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options

SQL>

--通过TNSNAMESORA连接:

在tnsnamesora 中配置如下:

cndba =

(DESCRIPTION =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192168110)(PORT = 1521))

(CONNECT_DATA =

(SERVER = DEDICATED)

(SERVICE_NAME = cndba)

)

)

--连接:

C:\Users\Dave>sqlplussystem/oracle@cndba

SQLPlus: Release 112010 Production on 星期三 4月 30 11:40:01 2014

Copyright (c) 1982, 2010, Oracle All rights reserved

连接到:

Oracle Database 12c Enterprise EditionRelease 121010 - 64bit Production

With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options

SQL>

2 在不同Container database中切换

在12c的架构中,因为有CDB和 PDB的存在,所以会有很多不同的container,所以在连接到不同的container时,就需要进行切换。

--查看当前的container:

SQL> show con_name

CON_NAME

------------------------------

CDB$ROOT

SQL> SELECT SYS_CONTEXT('USERENV','CON_NAME') FROM dual;

SYS_CONTEXT('USERENV','CON_NAME')

--------------------------------------------------------------------------------

CDB$ROOT

SQL> set lin 140

SQL> select con_id, dbid,guid, name , open_mode from v$pdbs;

CON_ID DBID GUID NAME OPEN_MODE

---------- ------------------------------------------ ---------- ----------

2 4088301206 F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED READ ONLY

3 426143573F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA READ WRITE

4 1231796139 F812DE1B6A8F363AE0430A01A8C0C759 PCNDBA2 READ WRITE

--切换container:

SQL> alter session setcontainer=pcndba2;

Session altered

SQL> show con_name

CON_NAME

------------------------------

PCNDBA2

3 连接到PDB

--EZCONNECT:

C:\Users\Dave>sqlplussystem/oracle@192168110:1521/pcndba2

SQLPlus: Release 112010 Production on 星期三 4月 30 11:54:30 2014

Copyright (c) 1982, 2010, Oracle All rights reserved

连接到:

Oracle Database 12c Enterprise EditionRelease 121010 - 64bit Production

With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options

SQL>

--TNSNAMESora

在tnsnamesora 中添加如下内容:

pcndba =

(DESCRIPTION =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192168110)(PORT = 1521))

(CONNECT_DATA =

(SERVER = DEDICATED)

(SERVICE_NAME = pcndba2)

)

)

C:\Users\Dave>sqlplussystem/oracle@pcndba

SQLPlus: Release 112010 Production on 星期三 4月 30 11:55:50 2014

Copyright (c) 1982, 2010, Oracle All rights reserved

连接到:

Oracle Database 12c Enterprise EditionRelease 121010 - 64bit Production

With the Partitioning, OLAP, AdvancedAnalytics and Real Application Testing options

SQL>

晕了 这明显是打不开session事务处理

有可能是hiber配置文件里的sessionFactory 配置问题

也有额能是你dao里面没有正确导入或执行transaction

既然你不在线 我先走了 明天再帮你解决 看下源码。

工作日可以Hi我

<property name="connectionpassword"></property>

为什么是 ? 文件中的是对的,你贴出来的时候改的吗?

以上就是关于怎样用Hibernate配置多个数据源全部的内容,包括:怎样用Hibernate配置多个数据源、求帮忙!!!用hibernate连接mysql数据库出现错误,密码无错误。、如何配置Hibernate连接oracle12c 的PDB和CDB数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9716902.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存