相关疑难解决方法(0)

Spring @Transactional - 隔离,传播

有人可以通过现实世界的例子解释注释中的隔离传播参数@Transactional.基本上何时以及为什么我应该选择更改其默认值.

java spring transactional isolation propagation

412
推荐指数
8
解决办法
25万
查看次数

你能在一个Hibernate会话中拥有多个交易吗?

你能在一个Hibernate会话中拥有多个交易吗?

我不清楚这是否是可取的.在我的代码中,我有一个长时间运行的线程并从阻塞队列中获取项目,具体取决于队列中的内容,它可能需要创建和保存一个hibernate对象,或者它可能不需要做任何事情.

每个项目都是不同的,所以如果项目1被保存,项目2无法保存我不想要的任何原因,以防止项目1被添加到数据库.

因此,最简单的方法是为每个需要创建的项目创建新会话,打开事务,保存新对象,提交事务,关闭会话

但是,这意味着为每个项目创建了一个新会话,这似乎违反了Hibernates自己的建议,即不执行会话每个请求模式.所以我的选择是在线程中创建一个会话,然后在需要创建新对象时根据需要打开并提交新事务.但我没有看到这种方法的例子,我不确定它是否真的有效.

java database session hibernate transactions

24
推荐指数
2
解决办法
4万
查看次数

Hibernate Session已关闭

当我调用方法session.begin事务时,如下所示:

//session factory is instantiated via a bean
Session session = this.getSessionFactory().getCurrentSession();
session.beginTransaction();
Run Code Online (Sandbox Code Playgroud)

然后我收到以下异常消息

6:13:52,217 ERROR [STDERR] org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)
Run Code Online (Sandbox Code Playgroud)

可能是导致此错误的原因是什么?

session hibernate

16
推荐指数
3
解决办法
7万
查看次数

为什么openSession不起作用,但getCurrentSession在Spring Hibernate中工作

我已经编写了一个示例Spring Hibernate应用程序o了解Spring hibernate集成的工作原理.

这是我的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <context:component-scan base-package="com.general" />

    <tx:annotation-driven />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
        <property name="username" value="system" />
        <property name="password" value="admin_123" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean ">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="com.general"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

然后,我的服务类就是这样

package …
Run Code Online (Sandbox Code Playgroud)

java hibernate spring-orm hibernate-4.x

4
推荐指数
1
解决办法
5927
查看次数

LazyInitializationException尝试获取懒惰的初始化实例

当我尝试获取懒惰的初始化实体时,我在IDE中看到以下异常消息(我无法找到它在代理实体中的存储位置,因此无法为该异常提供整个堆栈跟踪):

Method threw 'org.hibernate.LazyInitializationException' exception. Cannot evaluate com.epam.spring.core.domain.UserAccount_$$_jvste6b_4.toString()
Run Code Online (Sandbox Code Playgroud)

这是我尝试访问要使用的惰性初始化实体的字段后得到的堆栈跟踪:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)

    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)

    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)

    at com.epam.spring.core.domain.UserAccount_$$_jvstfc9_4.getMoney(UserAccount_$$_jvstfc9_4.java)

    at com.epam.spring.core.web.rest.controller.BookingController.refill(BookingController.java:128) 
Run Code Online (Sandbox Code Playgroud)

我正在使用Spring Data,已配置JpaTransactionManager,数据库是MySql,ORM提供程序是Hibernate4。注释@EnableTransactionManagement处于启用状态,@ Transactional随处可见,但无济于事。

这是一个关系:

@Entity
public class User extends DomainObject implements Serializable {

    ..

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "user_fk")
    private UserAccount userAccount;

    ..

@Entity
public class UserAccount extends DomainObject {

    ..

    @OneToOne(mappedBy = "userAccount")
    private User user;

    ..
Run Code Online (Sandbox Code Playgroud)

..一个配置:

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(); …
Run Code Online (Sandbox Code Playgroud)

mysql hibernate lazy-loading spring-transactions spring-data

4
推荐指数
1
解决办法
9206
查看次数

休眠:同时有两个或多个事务:事务已经处于活动状态

我有一个 REST API,当我几乎同时进行 POST 和 GET 时,我收到此异常:

 SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.IllegalStateException: Transaction already active
    at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
    at org.hibernate.internal.AbstractSharedSessionContract.beginTransaction(AbstractSharedSessionContract.java:409)
    at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:355)
    at com.sun.proxy.$Proxy58.beginTransaction(Unknown Source)
    at utils.HibernateSession.createTransaction(HibernateSession.java:15)
    at api.ConversationsREST.getMessages(ConversationsREST.java:128)
Run Code Online (Sandbox Code Playgroud)

它们位于不同的类上,因此没有隐含的全局属性。

失败的那一行是:

HibernateSession hs = new HibernateSession();
hs.createTransaction(); // Crash
Run Code Online (Sandbox Code Playgroud)

Wich 指的是我的类 HibernateSession:

public class HibernateSession {

    public Session session;

    public void createTransaction() {

        session = HibernateUtil.getSessionFactory().getCurrentSession(); //THIS WAS WRONG
 //EDIT:session = HibernateUtil.getSessionFactory().openSession(); //THIS …
Run Code Online (Sandbox Code Playgroud)

java rest hibernate jersey

4
推荐指数
1
解决办法
6964
查看次数

org.hibernate.HibernateException:没有活动事务,createSQLQuery无效

当我想通过hibernate连接到我的数据库时,我得到了这个异常,我正在尝试很多我在互联网上找到的东西,但没有任何帮助,我的一些文件:带有连接的dao类:

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    SessionFactory sessionFactory;
//the problem with query is here
    public List<User> getAllUsers() {
        return sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM user").list();
    }

}
Run Code Online (Sandbox Code Playgroud)

web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>
Run Code Online (Sandbox Code Playgroud)

我的servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.lime" /> …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate

2
推荐指数
1
解决办法
1万
查看次数

何时使用 OpenSession() 和 GetCurrentSession()

  1. OpenSession() 总是打开一个新会话。
  2. GetCurrentSession() 将返回绑定到上下文的会话

GetCurrentSession() 比 OpenSession() 快,所以我可以假设 GetCurrentSession 比 OpenSession() 好

在什么情况下我应该使用 openSession() 和 GetCurrentSession()

java hibernate

1
推荐指数
1
解决办法
2430
查看次数