使用Spring解决CLOB字段的存取

使用Spring解决CLOB字段的存取,记录一下,以备后用zzzzzzz...
首先使用网络上提供较多的解决办法,如下配置

<prop key="hibernate.connection.SetBigStringTryClob">true</prop>
<property name="context" type="text" column="context" />
private String context;//getter & setter 然后直接save BO :报错
Caused by: java.sql.SQLException: 不允许的操作: streams type cannot be used in batching
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.driver.OraclePreparedStatement.addBatch(OraclePreparedStatement.java:4073)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:172)
at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:53)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2275)
... 67 more


网友介绍,可以配置,<prop key="hibernate.jdbc.batch_size">0</prop>解决上述异常;系统里到处是批量提交,此方法不可取;
还有介绍:先保存CLOB字段为空,第二次update,却遇到CLOB类型转换异常;感觉挺麻烦,继续google[切换关键字组合];

[b]下面是成功简单解决办法[/b]

writeLog() ,调用者要声明事物,没必要把writeLog放到一个独立事物里(PROPAGATION_REQUIRES_NEW)或放到嵌套事物里;
saveXXX(){ --<prop key="save*">PROPAGATION_REQUIRED</prop>即可
//business
writeLog();
}
<!-- Spring-hibernate.xml -->
<bean id="nativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
lazy-init="true" />
<bean id="lobHandler"
class="org.springframework.jdbc.support.lob.OracleLobHandler"
lazy-init="true">
<property name="nativeJdbcExtractor">
<ref local="nativeJdbcExtractor" />
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="lobHandler"><ref bean="lobHandler"/></property>
....
</bean>
<!--BO.hbm.xml-->
<property name="context"
type="org.springframework.orm.hibernate3.support.ClobStringType"
column="context" />
<!-- BO.java-->
private String context; //getter & setter 然后就可以使用Hibernate保存BO:OK


如果不配置事物,会报下列异常

//Spring 处理CLOB,未配置事物
Caused by: java.lang.IllegalStateException: Active Spring transaction synchronization or active JTA transaction with specified [javax.transaction.TransactionManager] required
at org.springframework.jdbc.support.lob.LobCreatorUtils.registerTransactionSynchronization(LobCreatorUtils.java:79)
at org.springframework.orm.hibernate3.support.AbstractLobType.nullSafeSet(AbstractLobType.java:185)
at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:169)

文章引用: [url]http://space.itpub.net/10714335/viewspace-167565[/url]
环境:用jdk1.6.0_21+apache-tomcat-6.0.29+ojdbc14.jar+oracle10_db
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值