在几个选择查询或插入查询后休眠挂起

Posted

技术标签:

【中文标题】在几个选择查询或插入查询后休眠挂起【英文标题】:Hibernate hangs after several select queries or insert queries 【发布时间】:2016-08-09 12:39:31 【问题描述】:

我正在使用 Hibernate 关系开发 Spring 和 Hibernate Web 应用程序,它可以正常工作 3 次,并且从数据库中获取记录没有问题,但是在 4 次应用程序挂起或冻结时,当我重新启动 tomcat 服务器时,一切正常很好,尝试 4 次后再次挂起或冻结。当应用程序挂起或冻结让我很困惑时,没有错误。当我测试另一个在 Hibernate 上不使用关系运行良好的类时,我怀疑是关系,但我不知道~

这是我的 Hibernate 关系参考 ~

mkyong

viralpatel

这是我的任务,

这是我的用户类,

@SuppressWarnings("serial")
@Entity
@Table(name="user")
public class User implements Serializable

private String no_dana;
private String npp;

@Id
@Column(name="no_dana", unique=true, nullable=false, updatable=false)
public String getNo_dana() 
    return no_dana;

public void setNo_dana(String no_dana) 
    this.no_dana = no_dana;

@Column(name="npp")
public String getNpp() 
    return npp;

private Set<Tanya> tanya;

@OneToMany(fetch= FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user")
public Set<Tanya> getTanya() 
    return tanya;

public void setTanya(Set<Tanya> tanya) 
    this.tanya = tanya;


private Set<Jawab> jawab;

@OneToMany(fetch = FetchType.LAZY, mappedBy="user")
public Set<Jawab> getJawab() 
    return jawab;

public void setJawab(Set<Jawab> jawab) 
    this.jawab = jawab;


这是我的 tanya 课,

@SuppressWarnings("serial")
@Entity
@Table(name="tanya")
public class Tanya implements Serializable

private int id_tanya;
private String isi;

@Id
@Column(name="id_tanya", unique=true, nullable=false)
public int getId_tanya() 
    return id_tanya;

public void setId_tanya(int id_tanya) 
    this.id_tanya = id_tanya;

@Column(name="isi")
public String getIsi() 
    return isi;

public void setIsi(String isi) 
    this.isi = isi;

private User user;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="no_dana")
public User getUser() 
    return user;

public void setUser(User user) 
    this.user = user;


private Set<Jawab> jawab;

@OneToMany(fetch = FetchType.LAZY, mappedBy="tanya")
public Set<Jawab> getJawab() 
    return jawab;

public void setJawab(Set<Jawab> jawab) 
    this.jawab = jawab;


这是我的下巴班,

@SuppressWarnings("serial")
@Entity
@Table(name="jawab")
public class Jawab implements Serializable

private int id_jawab;
private String isi;

@Id
@Column(name="id_jawab", unique=true, nullable=false)
public int getId_jawab() 
    return id_jawab;

public void setId_jawab(int id_jawab) 
    this.id_jawab = id_jawab;

@Column(name="isi")
public String getIsi() 
    return isi;

public void setIsi(String isi) 
    this.isi = isi;


private User user;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="no_dana")
public User getUser() 
    return user;

public void setUser(User user) 
    this.user = user;


private Tanya tanya;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="id_tanya")
public Tanya getTanya() 
    return tanya;

public void setTanya(Tanya tanya) 
    this.tanya = tanya;


这是我的 dao 示例(来自 tanya 类),

@Autowired
private TanyaDaoImpl(SessionFactory sessionFactory)
    setSessionFactory(sessionFactory);


@SuppressWarnings("unchecked")
@Override
public Tanya get(int id_tanya)throws Exception
    DetachedCriteria criteria = DetachedCriteria.forClass(Tanya.class).add(Restrictions.eq("id_tanya", id_tanya));
    List<Tanya> tanyaList = getHibernateTemplate().findByCriteria(criteria);
    return tanyaList.get(0);


@Override
public Tanya getTanya(int id_tanya)throws Exception
    Query query = getSession().createQuery("from Tanya where id_tanya = :id_tanya");
    query.setParameter("id_tanya", id_tanya);
    return (Tanya) query.list().get(0);


@Override
public void save(Tanya tanya)throws Exception
    getHibernateTemplate().save(tanya);


@Override
public void update(Tanya tanya)throws Exception
    getHibernateTemplate().update(tanya);


@Override
public void delete(Tanya tanya)throws Exception
    getHibernateTemplate().delete(tanya);


@SuppressWarnings("unchecked")
@Override
public List<Tanya> listAllTanya()throws Exception
    DetachedCriteria criteria = DetachedCriteria.forClass(Tanya.class);
    List<Tanya> tanyaList = getHibernateTemplate().findByCriteria(criteria);
    return tanyaList;

这是我的会话配置,

<context:component-scan base-package="org.ppbni.splatter" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db_ppbni" />
    <property name="username" value="root" />
    <property name="password" value="shikamaru" />

    <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/ppbniorg_db" />
    <property name="username" value="ppbniorg_user" />
    <property name="password" value="shikamaru" /> -->

</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan" value="org.ppbni.splatter.model" />
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory">
</bean>

任何帮助都会很高兴 :D ~

【问题讨论】:

能否也补充一下如何指定dataSource bean? @alobodzk 我已经更新了我的问题,请查看~ 【参考方案1】:

这可能是因为以前的会话。在成功的事务后使用 session.close() 方法关闭会话,这将释放连接到数据库的实例。由于这种方法,问题得到了纠正。

session.close();

https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#close()

【讨论】:

【参考方案2】:

我不知道什么会导致间歇性问题,但有一些事情可以使用更正,也许这些更改会纠正行为。

几点:

    我的感觉是你应该把@IdLongInteger 并保持一致。

    您的 impl 有一些令人困惑的结构。为什么需要get()getTanya()

    在下面的示例中,我注释的是字段,而不是 getter。这似乎是 Spring 的偏好,尽管有 varying opinions。

    根据您提供的代码,我无法判断您是否包含@Transactional。无论如何,您都可以这样标记readOnly

    (旁白)如果不添加任何细节,则无需指定 FetchType.LAZY@Column 之类的默认值。只包含最基本的代码,这样下一个人就更容易阅读了。

除此之外,您还可以dump a stack trace 并查看线程挂在哪里。

通用 CRUD 接口:

public interface GenericDao<T> 

    T find(Long id);

    List<T> findAll();

    T update(T t);

    T save(T t);

    void delete(Long id);

谭雅道:

public interface TanyaDao extends GenericDao<Tanya> 

    //add any specific Tanya code

TanyaDAOImpl:

@Repository
@Transactional
public class TanyaDaoImpl implements TanyaDao 

    @Override
    @Transactional(readOnly = true, timeout = 10)
    public Tanya find(Integer id) 
        return (Tanya) getCurrentSession().get(Tanya.class, id);
    

    @Override
    @Transactional(readOnly = true, timeout = 10)
    public List<Tanya> findAll() 
        Criteria criteria = getCurrentSession().createCriteria(Tanya.class); // this may be different depending on your version of Hibernate
        @SuppressWarnings("unchecked")
        List<Tanya> tanyas = criteria.list();
        return tanyas;
    

    @Override
    public Tanya update(Tanya tanya) 
        try 
            getCurrentSession().update(tanya);
        
        catch (NonUniqueObjectException ignored) 
            tanya = (Tanya) getCurrentSession().merge(tanya); //you may or may not need to do this part
        
        return tanya;
    

    @Override
    public Tanya save(Tanya tanya) 
        Serializable id = getCurrentSession().save(tanya);
        return (Tanya) getCurrentSession().get(Tanya.class, id);
    

    @Override
    public void delete(Integer id) 
        Tanya tanya = find(id.intValue());
        getCurrentSession().delete(tanya);
    

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() 
        return sessionFactory.getCurrentSession();
    

用户:

@Entity
@Table(name = "user")
public class User implements Serializable 

    @Id
    @Column(name = "no_dana")
    @GeneratedValue
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Set<Tanya> tanya;

    @OneToMany(mappedBy = "user")
    private Set<Jawab> jawab;

    public Long getId() 

        return id;
    

    public void setId(Long id) 

        this.id = id;
    

    public Set<Tanya> getTanya() 

        return tanya;
    

    public void setTanya(Set<Tanya> tanya) 

        this.tanya = tanya;
    

    public Set<Jawab> getJawab() 

        return jawab;
    

    public void setJawab(Set<Jawab> jawab) 

        this.jawab = jawab;
    

贾瓦布:

@Entity
@Table(name = "jawab")
public class Jawab implements Serializable 

    @Id
    @GeneratedValue
    @Column(name = "id_jawab")
    private Long id;

    private String isi;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "no_dana")
    private User user;

    @ManyToOne
    @JoinColumn(name = "id_tanya")
    private Tanya tanya;

    public Long getId() 

        return id;
    

    public void setId(Long id) 

        this.id = id;
    

    public String getIsi() 

        return isi;
    

    public void setIsi(String isi) 

        this.isi = isi;
    

    public User getUser() 

        return user;
    

    public void setUser(User user) 

        this.user = user;
    

    public Tanya getTanya() 

        return tanya;
    

    public void setTanya(Tanya tanya) 

        this.tanya = tanya;
    


谭雅:

@Entity
@Table(name = "tanya")
public class Tanya implements Serializable 

    @Id
    @GeneratedValue
    @Column(name = "id_tanya")
    private Long id;

    private String isi;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "no_dana")
    private User user;

    @OneToMany(mappedBy = "tanya")
    private Set<Jawab> jawab;

    public Long getId() 

        return id;
    

    public void setId(Long id) 

        this.id = id;
    

    public String getIsi() 

        return isi;
    

    public void setIsi(String isi) 

        this.isi = isi;
    

    public User getUser() 

        return user;
    

    public void setUser(User user) 

        this.user = user;
    

    public Set<Jawab> getJawab() 

        return jawab;
    

    public void setJawab(Set<Jawab> jawab) 

        this.jawab = jawab;
    


【讨论】:

我已经删除了所有的关系,但在输入搜索时仍然遇到同样的问题,真的不知道 :DD ~

以上是关于在几个选择查询或插入查询后休眠挂起的主要内容,如果未能解决你的问题,请参考以下文章

在选择期间休眠重复的行

MySql InnoDB 可能的性能问题

带有子查询的休眠选择查询?

休眠查询语言或使用条件?

休眠日志显示更新查询,但没有选择

休眠选择查询不返回任何内容