I have a performance problem with Hibernate.
Hibernate : 3.2.6.ga
JDK : jdk1.6.0_45
I have a function witch is annotated @Transactionnal which is linked to an EntityManager.
This function is called in a loop so I have :
for (Item i : itemList)
{
saveIt(i);
}
It's ok if I launch it 5/10/20 times, the process time doesn't seem to increase. But if I launch it 300/400 times, the time to "saveit" is slower and slower ... I monitored the java memory and I didn't see something strange.
So I found some article talking about the Flush/Clean magic solution. I tried it and hourra, it works.
for (Item i : itemList)
{
saveit(i);
cleanMySession();
}
But to me it's quite strange, because I thought that the @Transactionnal annotation managed all this stuff, specially when I don't do anything else related to Hibernate outside this loop ... Maybe I am a little bit lost ...
Final question : is this workaround safe ?
Note : in reality, the saveIt function is quite huge in term of data manipulation, so the process time is quite important and must not increase.
EDIT - Additional information :
I stopped in debug mode in my cleanSession function :
public void cleanSession() {
Session session = (Session) em.getDelegate();
session.flush();
session.clear();
}
Here is the stack :
MyServiceImpl.cleanSession() line: 177
GeneratedMethodAccessor216.invoke(Object, Object[]) line: not available
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25
Method.invoke(Object, Object...) line: 597
AopUtils.invokeJoinpointUsingReflection(Object, Method, Object[]) line: 319
JdkDynamicAopProxy.invoke(Object, Method, Object[]) line: 196
$Proxy40.cleanSession() line: not available
MyAction.doSave() line: 814
StrutsStack...
So yes I see some Proxy but to me, this proxy comes from Spring Injection and not from Hibernate.
Edit N°2 :
Yes I use the OpenSessionInView filter
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>