日期:2014-05-20  浏览次数:21017 次

Hibnerate报数据大小超出此类型的最大值错误
我的配置是spring1.2+hibernate3+strut1.2,数据库Oracle9
数据源在tomcat中,配置为:
<Context path="/***" docBase="***" debug="0" reloadable="true">
 <Resource name="jdbc/***" auth="Container" 
  type="javax.sql.DataSource"
  username="***" password="***"
  driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@*.*.*.*:1521:***" 
  maxActive="4" maxIdle="2"/>
  <ResourceLink name="UserTransaction"
  global="UserTransaction"
  type="javax.transaction.UserTransaction"/>
</Context>
表的字段类型是varchar(4000);这里不能用clob,Blob(必要条件)
我想存入1000个汉字,实际输入900个就报这个错误:
- SQL Error: 17070, SQLState: null
- 数据大小超出此类型的最大值: 2700
- Could not synchronize database state with session
- [e] (HibernateTemplate): encountered SQLException [数据大小超出此类型的最大值: 2700]; nested exception is java.sql.SQLException: 数据大小超出此类型的最大值: 2700
org.springframework.jdbc.UncategorizedSQLException: (HibernateTemplate): encountered SQLException [数据大小超出此类型的最大值: 2700]; nested exception is java.sql.SQLException: 数据大小超出此类型的最大值: 2700
java.sql.SQLException: 数据大小超出此类型的最大值: 2700
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.ttc7.TTCItem.setArrayData(TTCItem.java:147)
at oracle.jdbc.dbaccess.DBDataSetImpl.setBytesBindItem(DBDataSetImpl.java:2461)
at oracle.jdbc.driver.OraclePreparedStatement.setItem(OraclePreparedStatement.java:1155)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:1572)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:131)
at net.sf.hibernate.type.StringType.set(StringType.java:26)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:48)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:35)
at net.sf.hibernate.persister.EntityPersister.dehydrate(EntityPersister.java:387)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:460)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:436)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
at net.sf.hibernate.transaction.JTATransaction.commit(JTATransaction.java:52)
at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:472)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:397)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:243)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
我试了用了Oracle10的ojdbc14.jar放在tomcat\common\lib下,可是不行。那位达人能提供些解决的方案,在下不胜感激!

------解决方案--------------------
根据网上一位网友的介绍,这是一个Oracle JDBC Driver的Bug。
原因是PreparedStatement的setString()会预检查数据类型,而这部分代码未考虑双字节字符的问题,所以就误报了。
解决方案:不用PreparedStatement,而直接组成sql语句插入。弊端是需要自己实现一个字符串转义过程,效率也稍微差那么一点点吧。