在运行Hibernate Hello World程序的时候,抛如下错误:
- Exception in thread "main" org.hibernate.exception.LockAcquisitionException : Could not open connection
- at org.hibernate.exception.internal.SQLStateConversionDelegate.convert( SQLStateConversionDelegate.java:144)
- at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert( StandardSQLExceptionConverter.java:49 )
- at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert( SqlExceptionHelper.java:125)
- at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert( SqlExceptionHelper.java:110)
- at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection( LogicalConnectionImpl.java:304)
- at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection( LogicalConnectionImpl.java:169)
- at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin( JdbcTransaction.java:67)
- at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin( AbstractTransactionImpl.java:160)
- at org.hibernate.internal.SessionImpl.beginTransaction( SessionImpl.java:1363)
- at com.ztesoft.hibernate.helloworld.StudentTest.main( StudentTest.java:25 )
- Caused by: java.sql.SQLException: The Network Adapter could not establish the connection
- at oracle.jdbc.driver.T4CConnection.logon( T4CConnection.java:412 )
- at oracle.jdbc.driver.PhysicalConnection.<init>( PhysicalConnection.java:531)
- at oracle.jdbc.driver.T4CConnection.<init>( T4CConnection.java:221 )
- at oracle.jdbc.driver.T4CDriverExtension.getConnection( T4CDriverExtension.java:32)
- at oracle.jdbc.driver.OracleDriver.connect( OracleDriver.java:503 )
- at java.sql.DriverManager.getConnection(Unknown Source)
- at java.sql.DriverManager.getConnection(Unknown Source)
- at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection( DriverManagerConnectionProviderImpl.java:192 )
- at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection( AbstractSessionImpl.java:278)
- at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection( LogicalConnectionImpl.java:297)
- ... 5 more
- Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
- at oracle.net.nt.ConnStrategy.execute( ConnStrategy.java:359 )
- at oracle.net.resolver.AddrResolution.resolveAndExecute( AddrResolution.java:422)
- at oracle.net.ns.NSProtocol.establishConnection( NSProtocol.java:672 )
- at oracle.net.ns.NSProtocol.connect( NSProtocol.java:237 )
- at oracle.jdbc.driver.T4CConnection.connect( T4CConnection.java:1042 )
- at oracle.jdbc.driver.T4CConnection.logon( T4CConnection.java:301 )
- ... 14 more
- Caused by: java.net.ConnectException: Connection refused: connect
- at java.net.PlainSocketImpl.socketConnect( Native Method )
- at java.net.PlainSocketImpl.doConnect(Unknown Source)
- at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
- at java.net.PlainSocketImpl.connect(Unknown Source)
- at java.net.SocksSocketImpl.connect(Unknown Source)
- at java.net.Socket.connect(Unknown Source)
- at oracle.net.nt.TcpNTAdapter.connect( TcpNTAdapter.java:141 )
- at oracle.net.nt.ConnOption.connect( ConnOption.java:123 )
- at oracle.net.nt.ConnStrategy.execute( ConnStrategy.java:337 )
- ... 19 more
通过plsql客户端发现oracle是可以连接的,为啥用hibernate来连接报连接拒绝呢?
找了半天发现是 hibernate.cfg.xml
- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!-- Database connection settings -->
- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="connection.url">jdbc:oracle:thin:@localhost:1522:orcl</property>
- <property name="connection.username">scott</property>
- <property name="connection.password">tiger</property>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="show_sql">true</property>
- <mapping resource="com/ztesoft/hibernate/helloworld/dto/Student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
配置文件中配置项
- <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
配置的有问题,由于之前在本地安装过两个oracle实例,现在正用的oracle实例的监听端口为 1522,把上面的配置项中的监听端口1521改成1522就OK了