5

I'm working on spring and hibernate web app using Hibernate Relationship, and it's working without problem 3 times and takes the records from database without problem but at 4 times the application hang or freezes, when I restart the tomcat server everything is fine and hangs or freeze again at 4 times trying. there are no error when the application hang or freezes that make me so confused. and when I test another class that not using relationship on Hibernate is running well, my suspect is the relationship but i have no idea ~

this is my Hibernate Relationship reference ~

mkyong

viralpatel

this is my erd,

enter image description here

this my user class,

@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;
}
}

this is my tanya class,

@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;
}
}

this is my jawab class,

@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;
}
}

and this is sample of my dao (from tanya class),

@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;
}

this is my session config,

<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>

any help will be pleasure :D ~

splatter_fadli
  • 761
  • 4
  • 16
  • 32

2 Answers2

2

This might be because of previous sessions.After successful transactions close the session using session.close() method which will free the instance connecting to database.The problem was rectified because of this method.

session.close();

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

Rakesh .p
  • 79
  • 2
  • 13
0

I don't readily see what would be causing an intermittent issue, but there are a few things that could use correction and perhaps the changes will correct the behavior.

Some points:

  1. My feeling is that you should make the @Id column either a Long or Integer and be consistent.

  2. Your impl has some confusing constructs. Why would you need both get() and getTanya()?

  3. In my examples below, I am annotating the fields, not the getters. This seems to be the Spring preference, though there are varying opinions.

  4. From your provided code, I can't tell whether you included @Transactional. Regardless, you can mark the readOnly ones as such.

  5. (Aside) You don't need to specify defaults like FetchType.LAZY or @Column if you're not adding any specifics. Only include the bare minimum code and it makes it easier for the next person to read.

Other than this, you can also dump a stack trace and see where the thread is hanging.

Generic CRUD interface:

public interface GenericDao<T> {

    T find(Long id);

    List<T> findAll();

    T update(T t);

    T save(T t);

    void delete(Long id);
}

TanyaDAO:

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();
    }
}

User:

@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;
    }
}

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;
    }

}

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;
    }

}
Community
  • 1
  • 1
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80