美好的一天! 我有一个c3po设置,当它被破坏时恢复与数据库(Oracle 11g)的连接 - 它可以工作
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<!--property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/-->
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider"/>
<property name="hibernate.connection.autoReconnect" value="true"/>
<!--максимальное количество соединений в пуле-->
<property name="hibernate.c3p0.max_size" value="10"/>
<!--минимальный размер пула-->
<property name="hibernate.c3p0.min_size" value="3"/>
<property name="hibernate.c3p0.acquire_increment" value="1"/>
<!--как долго ждать, чтобы подтвердить соединение, т.е. не закрывать его, а, например, сделать запрос "select 1"-->
<property name="hibernate.c3p0.idle_test_period" value="30"/>
<!-- In seconds -->
<property name="hibernate.c3p0.max_statements" value="50"/>
<!--таймаут для с3p0-->
<property name="hibernate.c3p0.timeout" value="0"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.jdbc.batch_size" value="50"/>
<!--limits how long a client will wait for a Connection default 0 Setting any positive value will cause the getConnection() call to time-out and break with an SQLException after the specified number of milliseconds -->
<property name="hibernate.c3p0.checkoutTimeout" value="0"/>
<property name="hibernate.c3p0.preferredTestQuery" value="select 1 from dual"/>
<property name="hibernate.c3p0.testConnectionOnCheckout" value="true"/>
<property name="hibernate.c3p0.testConnectionOnCheckIn" value="true"/>
<property name="hibernate.c3p0.acquireRetryAttempts" value="0"/>
<property name="hibernate.c3p0.acquireRetryDelay" value="5000"/>
<property name="hibernate.c3p0.breakAfterAcquireFailure" value="false"/>
</properties>
但现在我发现了一个错误。如果你设置了错误的ip数据库,应用程序就会进入等待状态。我们想给出一个合理的答案。请帮助。
答案 0 :(得分:1)
来自docs;
c3p0.acquireRetryAttempts=3
#Default: 30
#Defines how many times c3p0 will try to acquire a new Connection
#from the database before giving up. If this value is less than or
#equal to zero, c3p0 will keep trying to fetch a Connection
#indefinitely.
由于您不想无限期地尝试但返回错误消息,因此应将此值设置为大于0的值。如果无法重新连接断开的连接,则默认值30应该足够了在30次尝试中,事情可能会被打破到你想让别人知道的地步。
答案 1 :(得分:0)
我错了,我没有发现任何异常。当我设置acquireRetryAttempts = 1(例如)时,我的应用程序捕获下一个异常:
1)当我在运行应用程序之前从计算机中删除网络电缆时:
DBLogAppenderJPA::create() null
2453 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] WARN com.mchange.v2.resourcepool.BasicResourcePool - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1d47b2b -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (1). Last acquisition attempt exception:
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 com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
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)
... 12 more
Caused by: java.net.SocketException: Software caused connection abort: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
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)
... 17 more
2)当应用程序正在运行且hibernate和conn pool为init时,我从计算机中删除网络电缆。
Caused by: javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
at ru.sigmasoft.DoMessage.MessageProcessing.MessageBean.OracleMessageBean.OracleTableConsumer.listen(OracleTableConsumer.java:171)
... 4 more
Caused by: javax.persistence.PersistenceException: org.hibernate.TransactionException: commit failed
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80)
... 5 more
Caused by: org.hibernate.TransactionException: commit failed
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:185)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
... 5 more
Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:116)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:178)
... 6 more
Caused by: java.sql.SQLRecoverableException: IO Error: Connection reset
at oracle.jdbc.driver.T4CConnection.doCommit(T4CConnection.java:613)
at oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:3674)
at oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:3680)
at com.mchange.v2.c3p0.impl.NewProxyConnection.commit(NewProxyConnection.java:803)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112)
... 7 more
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at oracle.net.ns.Packet.receive(Packet.java:282)
at oracle.net.ns.DataPacket.receive(DataPacket.java:103)
at oracle.net.ns.NetInputStream.getNextPacket(NetInputStream.java:230)
at oracle.net.ns.NetInputStream.read(NetInputStream.java:175)
at oracle.net.ns.NetInputStream.read(NetInputStream.java:100)
at oracle.net.ns.NetInputStream.read(NetInputStream.java:85)
at oracle.jdbc.driver.T4CSocketInputStreamWrapper.readNextPacket(T4CSocketInputStreamWrapper.java:122)
at oracle.jdbc.driver.T4CSocketInputStreamWrapper.read(T4CSocketInputStreamWrapper.java:78)
at oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1179)
at oracle.jdbc.driver.T4CMAREngine.unmarshalSB1(T4CMAREngine.java:1155)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:279)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C7Ocommoncall.doOCOMMIT(T4C7Ocommoncall.java:75)
at oracle.jdbc.driver.T4CConnection.doCommit(T4CConnection.java:558)
... 11 more
在这两种情况下都没有连接恢复......但为什么呢?