赞
踩
Hibernate 4.2.7.SP1
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 64bit production
OJDBC7:12.1.0.1
数据:向数据库中插入一条数据,数据中某一个字段是CLOB类型,长度为4193个字符
报错异常:
- 2020-05-20 11:47:23,950 [qtp1288052401-61] c.p.p.m.d.ModelDao ERROR IOException occurred reading text
- org.hibernate.HibernateException: IOException occurred reading text
- at org.hibernate.type.descriptor.java.DataHelper.extractString(DataHelper.java:94) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.descriptor.java.DataHelper.extractString(DataHelper.java:280) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.descriptor.java.StringTypeDescriptor.wrap(StringTypeDescriptor.java:89) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.descriptor.java.StringTypeDescriptor.wrap(StringTypeDescriptor.java:39) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$1.doExtract(ClobTypeDescriptor.java:60) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:332) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2926) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1673) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1605) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.loader.Loader.getRow(Loader.java:1505) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:713
- Caused by: java.io.IOException: 违反协议: [ 14, 0, ]
- at oracle.jdbc.driver.OracleClobReader.needChars(OracleClobReader.java:264) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
- at oracle.jdbc.driver.OracleClobReader.read(OracleClobReader.java:195) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
- at org.hibernate.type.descriptor.java.DataHelper.extractString(DataHelper.java:86) ~[hibernate-core-4.2.7.SP1.jar:4.2.7.SP1]
- ... 119 more
- Caused by: java.sql.SQLException: 违反协议: [ 14, 0, ]
- at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:669) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
- at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
- at oracle.jdbc.driver.T4C8TTIClob.read(T4C8TTIClob.java:245) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
- at oracle.jdbc.driver.T4CConnection.getChars(T4CConnection.java:3901) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
- at oracle.sql.CLOB.getChars(CLOB.java:517) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
- at oracle.jdbc.driver.OracleClobReader.needChars(OracleClobReader.java:245) ~[ojdbc7-12.1.0.1.jar:12.1.0.1.0]
源码分析:
- public static String extractString(Reader reader, int lengthHint) {
- // read the Reader contents into a buffer and return the complete string
- final int bufferSize = getSuggestedBufferSize( lengthHint );
- final StringBuilder stringBuilder = new StringBuilder( bufferSize );
- try {
- char[] buffer = new char[bufferSize];
- while (true) {
- int amountRead = reader.read( buffer, 0, bufferSize );
- if ( amountRead == -1 ) {
- break;
- }
- stringBuilder.append( buffer, 0, amountRead );
- }
- }
- catch ( IOException ioe ) {
- throw new HibernateException( "IOException occurred reading text", ioe );
- }
- finally {
- try {
- reader.close();
- }
- catch (IOException e) {
- LOG.unableToCloseStream( e );
- }
- }
- return stringBuilder.toString();
- }
调试追踪到Hibernate在读取CLOB字段转换为String数据类型时,在read方法处抛出异常:int amountRead = reader.read( buffer, 0, bufferSize );
继续往里追踪,发现,read方法调用的是ojdbc7驱动中的OracleClobReader.read(char[] var1, int var2, int var3)方法:
- public int read(char[] var1, int var2, int var3) throws IOException {
- this.ensureOpen();
- int var5 = var2 + Math.min(var3, var1.length - var2);
- if (!this.needChars(var5 - var2)) {
- return -1;
- } else {
- int var4;
- for(var4 = var2 + this.writeChars(var1, var2, var5 - var2); var4 < var5 && this.needChars(var5 - var4); var4 += this.writeChars(var1, var4, var5 - var4)) {
- }
-
- return var4 - var2;
- }
- }
继续追踪,发现是在调用 needChars(int var1) 方法时抛出异常:
- protected boolean needChars(int var1) throws IOException {
- this.ensureOpen();
- if (this.pos >= this.count) {
- if (!this.endOfStream) {
- try {
- if (var1 > this.currentBufferSize) {
- this.currentBufferSize = Math.max(var1, this.initialBufferSize);
- PhysicalConnection var2 = (PhysicalConnection)this.clob.getInternalConnection();
- synchronized(var2) {
- this.resizableBuffer = var2.getCharBuffer(this.currentBufferSize);
- }
- }
-
- int var7 = this.currentBufferSize;
- if (this.maxPosition - this.lobOffset < (long)this.currentBufferSize) {
- var7 = (int)(this.maxPosition - this.lobOffset);
- }
-
- this.count = this.clob.getChars(this.lobOffset, var7, this.resizableBuffer);
- if (this.count < this.currentBufferSize) {
- this.endOfStream = true;
- }
-
- if (this.count > 0) {
- this.pos = 0;
- this.lobOffset += (long)this.count;
- if (this.lobOffset >= this.maxPosition) {
- this.endOfStream = true;
- }
-
- return true;
- }
- } catch (SQLException var6) {
- IOException var3 = DatabaseError.createIOException(var6);
- var3.fillInStackTrace();
- throw var3;
- }
- }
-
- return false;
- } else {
- return true;
- }
- }
经过对逻辑分析与调试,发现4193长度的字符串调用getInternalConnection会抛出异常,但是加大了了字符串长度到8193后,异常不再抛出,于是怀疑是OJDBC7这个版本的bug,升级到OJDBC7:12.1.0.2后,问题得到解决
至于12.1.0.1版本字符串长度为4193时,调用getInternalConnection()方法后会抛出异常的原因还未深究,感兴趣的同学可以了解一下源码,看一下具体问题出现的原因。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。