Bonjour,

j'ai de nombreux inserts à faire et je voudrais utiliser un batch.
J'ai configuré hibernate.jdbc.batch_size à 50.
Je vois bien dans les logs au démarrage :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
[10:53:07,609 ] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 50
mon code est le suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
			for(int i=0; i<uids.size(); i++) {
				String uid = uids.get(i);
 
				for(int j=0; j<idsRubriques.size(); j++) {
					Integer idRubrique = idsRubriques.get(j);
					AutorisationBean autorisation = new AutorisationBean();
					autorisation.setUserUid(uid);
					RubriqueBean rubrique = new RubriqueBean();
					rubrique.setRubriqueId(idRubrique);
					autorisation.setRubriqueBean(rubrique);
					autorisation.setDateCreation(new Date());
					autorisation.setLoginCreation("abo_auto");
					autorisation.setDateModification(new Date());					
 
					try	{
						logger.info("Abonnement de l'utilisateur " + uid + " à la rubrique " + idRubrique);
 
						this.getHibernateTemplate().save(autorisation);						
 
						if (((i*idsRubriques.size())+(j+1)) % 50 == 0) { //50, même taille que la taille du paquet JDBC
							//flush un paquet d'insertions et libère la mémoire :
							this.getHibernateTemplate().flush();
							this.getHibernateTemplate().clear();
						}
					} catch (DataAccessException dae) {						
						if(dae.getCause() instanceof ConstraintViolationException) {
							// si c'est une exception de contrainte, on continue...
							logger.warn("Utilisateur " + uid + " est déjà abonné à la rubrique " + idRubrique);
						} else {
							// sinon on sort...
							throw dae;
						}
					} 
				}
			}
Et quand j'execute ce code, il me fait les insert un à un (je le vois bien en me mettant en debug et en vérifiant l'état de la base à chaque itération de la boucle.
Dans les logs :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[11:07:48,312 ] INFO fr.su.unv.asu.egeri.dao.impl.AutorisationsDaoImpl - Abonnement de l'utilisateur U2006-02-17-14.42.59.516000 à la rubrique 23027
[11:07:48,656 ] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 12301132686
[11:07:48,656 ] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[11:07:48,656 ] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
[11:07:48,656 ] DEBUG org.hibernate.SQL - select SEQ_AUID.nextval from dual
[11:07:48,656 ] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: 1379038
[11:07:48,656 ] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[11:07:48,656 ] DEBUG org.hibernate.event.def.AbstractSaveEventListener - generated identifier: 1379038, using strategy: org.hibernate.id.SequenceGenerator
[11:07:48,656 ] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[11:07:48,671 ] DEBUG org.hibernate.SQL - select rubriquebe_.RU_ID, rubriquebe_.AP_ID as AP2_17_, rubriquebe_.RU_IDPERE as RU3_17_, rubriquebe_.RU_LIBELLE as RU4_17_, rubriquebe_.RU_BG as RU5_17_, rubriquebe_.RU_BD as RU6_17_, rubriquebe_.RU_NIVEAU as RU7_17_, rubriquebe_.RU_CONSULTABLE as RU8_17_, rubriquebe_.RU_PUBLIABLE as RU9_17_, rubriquebe_.RU_HORSMENU as RU10_17_, rubriquebe_.RU_TYPE as RU11_17_, rubriquebe_.RU_ICONE as RU12_17_, rubriquebe_.RU_CIBLE as RU13_17_, rubriquebe_.RU_LIEN as RU14_17_, rubriquebe_.RU_MAGASIN as RU15_17_, rubriquebe_.RU_DATECRE as RU16_17_, rubriquebe_.RU_LOGINCRE as RU17_17_, rubriquebe_.RU_DATEMOD as RU18_17_, rubriquebe_.RU_LOGINMOD as RU19_17_, rubriquebe_.RU_ACCES as RU20_17_ from RU_RUBRIQUE rubriquebe_ where rubriquebe_.RU_ID=?
[11:07:48,687 ] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[11:07:48,687 ] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades
[11:07:48,687 ] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections
[11:07:48,687 ] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
[11:07:48,687 ] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
[11:07:48,687 ] DEBUG org.hibernate.pretty.Printer - listing entities:
[11:07:48,687 ] DEBUG org.hibernate.pretty.Printer - fr.su.unv.asu.egeri.metier.AutorisationBean{dateCreation=24 décembre 2008, userUid=U2006-02-17-14.42.59.516000, rubriqueBean=fr.su.unv.asu.egeri.metier.RubriqueBean#23027, autorisationId=1379038, loginModification=null, dateModification=24 décembre 2008, loginCreation=abo_auto}
[11:07:48,687 ] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[11:07:48,687 ] DEBUG org.hibernate.SQL - insert into AU_AUTORISATION (AU_UID, RU_ID, AU_DATECRE, AU_LOGINCRE, AU_DATEMOD, AU_LOGINMOD, AU_ID) values (?, ?, ?, ?, ?, ?, ?)
[11:07:48,687 ] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
[11:07:48,703 ] DEBUG org.hibernate.jdbc.Expectations - success of batch update unknown: 0
[11:07:48,703 ] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[11:07:48,703 ] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
[11:07:48,703 ] DEBUG org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
Comment faire pour qu'il ne fasse l'insertion que toutes les 50 requetes ?