-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: BatchUpdateException Duplicate key
PostPosted: Thu Jan 19, 2006 9:49 am 
Newbie

Joined: Fri Jul 22, 2005 8:47 am
Posts: 8
Hibernate version: 2.1.8

Hi.
I take the following exception
Exception:
18-01-2006 22:57:51 DEBUG [JDBCExceptionReporter.logExceptions:49] Could not execute JDBC batch update
java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '541161475169' for key 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1540)
at com.mchange.v2.sql.filter.FilterPreparedStatement.executeBatch(FilterPreparedStatement.java:260)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:128)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
at com.mjoy.dcmbase.persistence.PersistenceManager.save(PersistenceManager.java:217)
at com.mjoy.wapdcm.persistence.PersistenceLayer.guardarUsuario(PersistenceLayer.java:209)
at com.mjoy.wapdcm.handler.BussinesLogic.saveOrUpdateUser(BussinesLogic.java:141)
at com.mjoy.wapdcm.actions.ConfirmacionAction.performAction(ConfirmacionAction.java:111)
at com.mjoy.wapdcm.actions.AbstractAction.execute(AbstractAction.java:54)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:535)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
18-01-2006 22:57:51 DEBUG [NullableType.nullSafeGet:68] returning '5' as column: id_sub_t8_
18-01-2006 22:57:51 WARN [JDBCExceptionReporter.logExceptions:57] SQL Error: 1062, SQLState: 23000
18-01-2006 22:57:51 DEBUG [NullableType.nullSafeGet:68] returning '57' as column: id_telco9_
18-01-2006 22:57:51 ERROR [JDBCExceptionReporter.logExceptions:58] Duplicate key or integrity constraint violation message from server: "Duplicate entry '541161475169' for key 1"
18-01-2006 22:57:51 DEBUG [NullableType.nullSafeGet:68] returning 'Descarga de Wallpapers $3' as column: nombre
18-01-2006 22:57:51 ERROR [SessionImpl.execute:2400] Could not synchronize database state with session



At this our on my application i recive 70 hits per second. I configure hibernate with ehcache to perform the load of application, but because growing by day, start to throw this exception.

I was read the hibernate forum and other pages and i try the next modifications but can't solve problem:

1- Isolation level 2, 4 and 1 with LockMode.UPGRADE.
2- Disable Batch-Update mode with hibernate.batch.size = 0
3- An now enable the FlushMode.COMMIT in method com.mjoy.wapdcm.persistence.PersistenceLayer.guardarUsuario(PersistenceLayer.java:209)
like be seem the cause of the error.

I Post the hibernate.cfg.xml perhapes help us.

Mapping documents:

<!-- Mapping general for all policies -->
<property name="jdbc.use_streams_for_binary">true</property>
<property name="show_sql">false</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.cache.provider_class">
net.sf.hibernate.cache.EhCacheProvider
</property>^M
<property name="hibernate.cache.use_query_cache">true</property>^M
<property name="hibernate.cglib.use_reflection_optimizer">true</property>^M
<property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>^M
<property name="connection.isolation">1</property>
<!-- End Mapping General -->

<!-- Mapping files -->
<mapping resource="com/mjoy/dcmbase/entities/Sesion.hbm.xml" />
<mapping resource="com/mjoy/dcmbase/entities/CanalVenta.hbm.xml" />
<mapping resource="com/mjoy/dcmbase/entities/MensajeCustom.hbm.xml" />
<mapping resource="com/mjoy/dcmbase/entities/Contenido.hbm.xml" />
<mapping resource="com/mjoy/dcmbase/entities/Categoria.hbm.xml" />
<mapping resource="com/mjoy/dcmbase/entities/Cliente.hbm.xml" />






Name and version of the database you are using:
MySQL 4.0.x


Thanks for you help.
Juan Pablo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 10:15 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
could you give some information about the mapping etc.

is the exception only happing for user or also for other methods.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 10:28 am 
Newbie

Joined: Fri Jul 22, 2005 8:47 am
Posts: 8
Hi,

Not only happend with method guardarUsuario()
I Post you the method detail.


public Usuario guardarUsuario(Usuario user,Terminal terminal){
log.debug("Verificando si el usuario existe en la base de datos");
Usuario persistente = (Usuario)PersistenceManager.get(Usuario.class,user.getLinea());
//PersistenceManager.lockPessimistic(persistente,LockMode.UPGRADE);
log.debug("Usuario existe en la base? - "+existUser(persistente));
PersistenceManager.getSession().setFlushMode(FlushMode.COMMIT);
if(existUser(persistente)){
log.debug("Existe usuario - Actualizando .................");
persistente.setFechaActualizacion(new Date());
log.debug("Actualizando fecha del usuario");
persistente.setTelco(user.getTelco());
log.debug("Actualizando telco de acuerdo al portal ");
persistente.setTerminal(terminal);
log.debug("Seteando nueva terminal de acuerdo al userAgent");
PersistenceManager.update(persistente);
return persistente;
}else{
log.debug("Persistiendo nuevo usuario..........");
PersistenceManager.save(user);
log.debug("Usuario insertado en la base........");
return user;
}
}



The next code is the method PersistenceManager.update() look you see how i update object

public static void update(Object obj) {
try {
beginTransaction();
log.debug("Updating object - " + obj.getClass().getName());
getSession().update(obj);
} catch (HibernateException e) {
log.debug("Error updating object - " +
obj.getClass().getName() + " \n " + e.getMessage());
throw new DCMHibernateException(e);
}
}


This is the mapping file of "Usuario.class":

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
<class
name="com.mjoy.dcmbase.entities.Usuario"
table="usuarios"
>

<id
name="linea"
column="linea"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="assigned">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Usuario.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<property
name="fechaActualizacion"
type="java.util.Date"
update="true"
insert="true"
column="fecha_actualizacion"
not-null="false"
/>

<property
name="fechaAlta"
type="java.util.Date"
update="true"
insert="true"
column="fecha_alta"
not-null="false"
/>

<many-to-one
name="telco"
class="com.mjoy.dcmbase.entities.Telco"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="id_telco"
/>

<many-to-one
name="terminal"
class="com.mjoy.dcmbase.entities.Terminal"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="id_terminal"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Usuario.xml
containing the additional properties and place it in your merge dir.
-->

</class>

<query name="BuscarUsuario"><![CDATA[
from Usuario as u where u.linea = :linea
]]></query>

</hibernate-mapping>


If you need some information about something i do in my application, tell me.

Thanks for your help

Juan Pablo


Top
 Profile  
 
 Post subject: Not reply yet
PostPosted: Tue Jan 24, 2006 9:30 am 
Newbie

Joined: Fri Jul 22, 2005 8:47 am
Posts: 8
Please i need some answer about this problem or any possible change could make in my application.

Other problem i have with this application is the isolation level.

1) What level isolation you recommend for this application that work with ehcache read-write level?

2) Is necesary call LockMode.XXXX or only with mapping hibernate.cfg.xml property is enough?

Thanks for you answer.

Juan Pablo


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 9:49 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello jper19,

I do not think that isolation level will change anything here.

You assign the primary key to User. How do you generate this key. If your generator is bad you could hit a double key from time to time. Why don't you use increment as generator type. (for Hibernate 2 the generator type for increment fields could have a different name) .

Is your key generator thread save?

I suppose the exception happens in the else path of your method. You should also consider to extract this to a method to see properly in the logfiles, where this happens. Normally this should be of course the generation of a user.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject: Reply
PostPosted: Tue Jan 24, 2006 10:03 am 
Newbie

Joined: Fri Jul 22, 2005 8:47 am
Posts: 8
Thank for your help, but the generation key is assigned because is a telephone line number, and no is generate by any mechanism.

The exception only throws in the if clause. When the else clause is execute, the user always is save fine.

If you get any idea wath happend i appreciate.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 10:40 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Just an idea,
your update method is not commiting a transaction.

I would not use Read uncommited in any application but Read commited.

Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.