解决load,get,save 等is not valid without active transaction异常的原因。

简介: org.hibernate.HibernateException:*** is not valid without active transaction 2010-12-09 15:36:07标签:休闲is not valid without active 生活Hibernate职场 Exception in thread "main" org.hibernate.HibernateExc
org.hibernate.HibernateException:*** is not valid without active transaction 2010-12-09 15:36:07

Exception in thread "main" org.hibernate.HibernateException: *** is not valid without active transaction
 at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
 at $Proxy13.getListeners(Unknown Source)
 at org.hibernate.search.backend.impl.EventSourceTransactionContext.getIndexWorkFlushEventListener(EventSourceTransactionContext.java:78)
 at org.hibernate.search.backend.impl.EventSourceTransactionContext.<init>(EventSourceTransactionContext.java:41)
 at org.hibernate.search.impl.FullTextSessionImpl.<init>(FullTextSessionImpl.java:75)
 at org.hibernate.search.Search.getFullTextSession(Search.java:23)
 at com.yin.hibernate.model.GuestBookTest.main(GuestBookTest.java:28)

在网上找的原因是:
如果使用hibernate的getCurrentSession()获得session对象,对查询语句也需要开启事务,不然就会抛出如上异常!
解决方法

加上  Transaction tx = session.beginTransaction(); 
      tx.commit();
注意:在 hibernate.cfg.xml
如果采用jdbc独立引用程序配置如下:
<property name=”hibernate.current_session_context_class”>thread</property>
如果采用了JTA事务配置如下
<property name=”hibernate.current_session_context_class”>jta</property>

目录
相关文章
Warning: Can save best model only with val_acc available, skipping
本文解决了在使用DenseNet网络结构保存最优模型时出现的"Warning: Can save best model only with val_acc available, skipping"问题,方法是将`ModelCheckpoint`回调函数中的`monitor`参数值从`val_acc`改为`val_accuracy`。
|
5月前
|
Oracle 关系型数据库 数据库
Active Data Guard Real-Time Cascade
12c 的 Cascaded Standby 数据库
51 7
|
7月前
|
开发工具 git
解决pre-commit hook failed (add --no-verify to bypass)的问题
该文介绍了两种免去Git预提交钩子(pre-commit)的方法。一是直接进入项目.git/hooks目录,使用`rm -rf ./git/hooks/pre-commit`命令删除pre-commit文件。二是提交时添加`--no-verify`参数,如`git commit --no-verify -m&quot;XXX&quot;`,以跳过预提交检查。
268 0
|
缓存 关系型数据库 MySQL
【异常解决】缓存报错:Null key returned for cache operation (maybe you are using named params on classes withou
【异常解决】缓存报错:Null key returned for cache operation (maybe you are using named params on classes withou
712 0
|
数据安全/隐私保护
Do Sync Disk 0 Part 0 Failed, code=S3_F42, msg=Sync Failed after retry 5 times
Do Sync Disk 0 Part 0 Failed, code=S3_F42, msg=Sync Failed after retry 5 times
192 1
|
索引
ES报错:“type“=>“cluster_block_exception“, “reason“=>“blocked by: [FORBIDDEN/12/index read-only / allow
ES报错:“type“=>“cluster_block_exception“, “reason“=>“blocked by: [FORBIDDEN/12/index read-only / allow
369 0
ES报错:“type“=>“cluster_block_exception“, “reason“=>“blocked by: [FORBIDDEN/12/index read-only / allow
|
存储 NoSQL Linux
redis异常 Commands that may modify the data set are disabled, because this instance is
MISCONF Redis配置为保存RDB快照,但目前无法在磁盘上持久化。可能修改数据集的命令被禁用,因为该实例被配置为在RDB快照失败时报告错误(stop-write -on-bgsave-error选项)。请检查Redis的日志RDB错误的详细信息.
redis异常 Commands that may modify the data set are disabled, because this instance is
|
数据库连接 Linux API
R-Retrieve Data (step 1)
R is a data analysis and visualization platform.
1220 0
|
安全
Error Code: 1175. You are using safe update mode and you tried to update a t
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inforstack/article/details/79677217 在安全模式下,只能根据主键来做修改,所以使用非主键修改,那么将要解除安全模式,然后再执行操作。
1650 0