[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.0

On simultaneous request "object references an unsaved transient instance" error problem

Asked by TheFACTORY_Max in Java Programming Language, Spring, J2EE Frameworks

Tags: hibernate, spring, struts, session, transient, unsaved

Hello experts,

I have yet another hibernate question, I have an application that works with hibernate + spring MVC + struts. The problem I am having is with, I think, the sessions and detached objects.

When I test the application with one user it works flawlessly, however I have now started some stress testing using JMeter which can simulate as many simultaneous visits as you want, while trying this with 10 simultaneous visits at the time it gives an hibernate error every now and then.
The error given on the page is the following:

javax.servlet.ServletException: org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.GolfOnline.Beans.Course; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.GolfOnline.Beans.Course

The stack gives this as error:
object references an unsaved transient instance - save the transient instance before flushing: com.GolfOnline.Beans.Course

and refers to a very simple query in the GolferTypeDAO which goes wrong:
ArrayList results = (ArrayList)getHibernateTemplate().find("from GolferType where course = ? and title='Guest'", course);
(where course is a reference to a 'Course' object retrieved earlier in the same request)

As you can see I use the getHibernateTemplate helper class http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/orm/hibernate/HibernateTemplate.html
The spring application context holds the SessionFactory and gives this to the available DAO's, where the getHibernateTemplate handles the sessions from.

I've searched the error and found many stories telling about the sessionfactory, sessions, transactions and different patterns (such as the session-per-request, session-per-request-with-detached-objects, session-per-conversation , open-session-in-view-pattern)

Firstly I am not exactly sure why it gives me this error, I think it is because in one request I retrieve a 'Course' object and in this same request I use this to retrieve a 'GolferType' object using an association to a 'Course' object. With many simultaneous requests at the same time the first retrieved 'Course' object becomes detached for some reason and can't be used again in a query then?
This is easily solved by using the ID's in the query but It does not feel good as this is more of a workaround instead of actually solving the problem (which probably will come back somewhere else)

Could you advise me on which way to go in order to solve the problem?
All relevant code is below and I will give any addition information if needed.


Thanks in advance,
Max
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
------------------------------------------------------- applicationContext.xml
 
<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <!-- Hibernate (persistence) -->
    
    <bean id="golfOnlineDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         --censored--
</bean>
 
    <bean id="golfOnlineSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="golfOnlineDataSource"/>
        <property name="mappingResources">
            <list>
                <value>com/GolfOnline/Hibernate/Course.hbm.xml</value>
                <value>com/GolfOnline/Hibernate/GolferType.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.connection.pool_size">0</prop>
                <prop key="hibernate.dbcp.maxActive">15</prop>
                <prop key="hibernate.dbcp.maxIdle">5</prop>
                <prop key="hibernate.dbcp.maxWait">120000</prop>
                <prop key="hibernate.dbcp.whenExhaustedAction">1</prop>
                <prop key="hibernate.dbcp.testOnBorrow">true</prop>
                <prop key="hibernate.dbcp.testOnReturn">true</prop>
                <prop key="hibernate.dbcp.validationQuery">--censored--</prop>
                <prop key="hibernate.dbcp.ps.maxActive">0</prop>
                <prop key="hibernate.dbcp.ps.maxIdle">0</prop>
                <prop key="hibernate.dbcp.ps.maxWait">-1</prop>
                <prop key="hibernate.dbcp.ps.whenExhaustedAction">2</prop>
            </props>
        </property>
    </bean>
    
    <bean id="golfOnlineTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="golfOnlineSessionFactory"/>
        </property>
    </bean>
    
    <!-- Service objects -->
    <bean id="courseService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">	
        <property name="transactionManager"><ref local="golfOnlineTransactionManager"/></property>
        <property name="target"><ref local="courseServiceTarget"/></property>
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="add*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    
    <!-- Service target objects -->
    <bean id="courseServiceTarget" class="com.GolfOnline.Services.CourseServiceImpl">
        <property name="courseDAO"><ref local="courseDAO"/></property>
        <property name="golferTypeDAO"><ref local="golferTypeDAO"/></property>
    </bean>
    
    <!-- DAO objects -->
    <bean id="courseDAO" class="com.GolfOnline.DAOs.CourseDAOImpl">
        <property name="sessionFactory" ref="golfOnlineSessionFactory"/>
    </bean>
    <bean id="golferTypeDAO" class="com.GolfOnline.DAOs.GolferTypeDAOImpl">
        <property name="sessionFactory" ref="golfOnlineSessionFactory"/>
    </bean>
    
</beans>
 
------------------------------------------------------- course.hbm
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.GolfOnline.Beans.Course" table="tbl_Course">
    <id column="cr_ID" name="id" type="short">
      <generator class="native"/>
    </id>
    <property column="cr_Name" name="name"/>
  </class>
</hibernate-mapping>
 
------------------------------------------------------- golfertype.hbm
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.GolfOnline.Beans.GolferType" table="tbl_GolferType">
    <id column="gt_ID" name="id" type="short">
      <generator class="native"/>
    </id>
    <many-to-one class="com.GolfOnline.Beans.Course" column="gt_Course_cr_ID" name="course"/>
    <property column="gt_Title" name="title"/>
  </class>
</hibernate-mapping>
[+][-]03/27/09 08:15 PM, ID: 24007092Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/27/09 08:16 PM, ID: 24007094Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/28/09 01:05 PM, ID: 24009926Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/30/09 04:07 AM, ID: 24017642Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/02/09 03:47 AM, ID: 24048276Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/14/09 02:38 AM, ID: 24136295Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04/17/09 07:58 PM, ID: 24173350Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04/20/09 07:04 PM, ID: 24190293Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04/25/09 06:54 PM, ID: 24234300Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04/25/09 06:54 PM, ID: 24234301Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04/27/09 01:49 AM, ID: 24239822Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/11/09 01:22 AM, ID: 24352415Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Programming Language, Spring, J2EE Frameworks
Tags: hibernate, spring, struts, session, transient, unsaved
Sign Up Now!
Solution Provided By: TheFACTORY_Max
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625