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 package fr.paris.lutece.util.sql;
35
36 import fr.paris.lutece.portal.service.database.AppConnectionService;
37 import fr.paris.lutece.portal.service.database.PluginConnectionService;
38 import fr.paris.lutece.portal.service.plugin.Plugin;
39 import fr.paris.lutece.portal.service.util.AppException;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.portal.service.util.NoDatabaseException;
42
43 import org.apache.log4j.Logger;
44
45 import org.springframework.jdbc.datasource.DataSourceUtils;
46
47 import org.springframework.transaction.support.TransactionSynchronizationManager;
48
49 import java.io.InputStream;
50
51 import java.sql.Blob;
52 import java.sql.Connection;
53 import java.sql.Date;
54 import java.sql.PreparedStatement;
55 import java.sql.ResultSet;
56 import java.sql.SQLException;
57 import java.sql.Time;
58 import java.sql.Timestamp;
59 import java.sql.Types;
60
61 import java.text.MessageFormat;
62
63 import javax.sql.DataSource;
64
65
66
67
68
69
70
71 public class DAOUtil
72 {
73 public static final String MSG_EXCEPTION_SELECT_ERROR = "Error selecting row id : ";
74 private static final String DEFAULT_MODULE_NAME = "lutece";
75 private static final String LOGGER_DEBUG_SQL = "lutece.debug.sql.";
76
77
78 private PluginConnectionService _connectionService;
79
80
81 private Connection _connection;
82
83
84 private String _strPluginName;
85
86
87 private PreparedStatement _statement;
88
89
90 private ResultSet _resultSet;
91
92
93 private boolean _bReleased;
94 private String _strSQL;
95 private boolean _bTransactionnal;
96
97
98 private Logger _logger;
99 private StringBuffer _sbLogs = new StringBuffer( );
100
101
102
103
104
105
106 public DAOUtil( String sql )
107 {
108 this( sql, null );
109 }
110
111
112
113
114
115
116
117 public DAOUtil( String strSQL, Plugin plugin )
118 {
119 _bReleased = false;
120 _strSQL = strSQL;
121
122 if ( plugin != null )
123 {
124 _strPluginName = plugin.getName( );
125 _connectionService = plugin.getConnectionService( );
126 }
127 else
128 {
129 _strPluginName = DEFAULT_MODULE_NAME;
130 _connectionService = AppConnectionService.getDefaultConnectionService( );
131 }
132
133 if ( _connectionService == null )
134 {
135 throw new NoDatabaseException(
136 "Database access error. Please check component installations and db.properties." );
137 }
138
139
140 _logger = Logger.getLogger( LOGGER_DEBUG_SQL + _strPluginName );
141
142 if ( _logger.isDebugEnabled( ) )
143 {
144 log( "Module : '" + _strPluginName + "' - SQL Statement : " + _strSQL );
145 }
146
147 try
148 {
149 MultiPluginTransaction transaction = TransactionManager.getCurrentTransaction( plugin );
150
151 if ( transaction != null )
152 {
153 _bTransactionnal = true;
154 }
155 else
156 {
157
158 if ( TransactionSynchronizationManager.isSynchronizationActive( ) )
159 {
160 _bTransactionnal = true;
161
162 DataSource ds = AppConnectionService.getPoolManager( )
163 .getDataSource( _connectionService.getPoolName( ) );
164 _connection = DataSourceUtils.getConnection( ds );
165
166 if ( _logger.isDebugEnabled( ) )
167 {
168 _logger.debug( "Transactionnal context is used for pool " + _connectionService.getPoolName( ) );
169 }
170 }
171 else
172 {
173
174 _connection = _connectionService.getConnection( );
175 }
176 }
177
178 if ( transaction != null )
179 {
180 _statement = transaction.prepareStatement( _strSQL );
181 }
182 else if ( _connection != null )
183 {
184 _statement = _connection.prepareStatement( _strSQL );
185 }
186 else
187 {
188 throw new AppException( "Database access error for component '" + _strPluginName +
189 "'. Please check plugin installation and db.properties." );
190 }
191 }
192 catch ( SQLException e )
193 {
194 free( );
195 throw new AppException( getErrorMessage( e ), e );
196 }
197 }
198
199
200
201
202
203
204 private String getErrorMessage( Exception e )
205 {
206 free( );
207
208 StringBuilder sbError = new StringBuilder( "DAOUtil error : " );
209 sbError.append( e.getMessage( ) );
210 sbError.append( " - SQL statement : " );
211 sbError.append( " - Plugin : " );
212 sbError.append( _strPluginName );
213
214 return sbError.toString( );
215 }
216
217
218
219
220 public void executeUpdate( )
221 {
222 try
223 {
224 _statement.executeUpdate( );
225 }
226 catch ( SQLException e )
227 {
228 free( );
229 throw new AppException( getErrorMessage( e ), e );
230 }
231 }
232
233
234
235
236 public void executeQuery( )
237 {
238 try
239 {
240 _resultSet = _statement.executeQuery( );
241 }
242 catch ( SQLException e )
243 {
244 free( );
245 throw new AppException( getErrorMessage( e ), e );
246 }
247 }
248
249
250
251
252
253 private void log( String strMessage )
254 {
255 if ( _logger.isDebugEnabled( ) )
256 {
257 _sbLogs.append( strMessage );
258 }
259 }
260
261
262
263
264
265
266 private void logParameter( Object oName, Object oValue )
267 {
268 Object[] args = { oName, oValue };
269 log( MessageFormat.format( "\n Index : ''{0}'' Value : ''{1}'' ", args ) );
270 }
271
272
273
274
275 private void writeLogs( )
276 {
277 if ( _logger.isDebugEnabled( ) )
278 {
279 _logger.debug( _sbLogs.toString( ) );
280 }
281 }
282
283
284
285
286 public final void free( )
287 {
288 writeLogs( );
289
290 try
291 {
292
293 if ( _statement != null )
294 {
295 _statement.close( );
296 }
297 }
298 catch ( SQLException e )
299 {
300 throw new AppException( e.getMessage( ), e );
301 }
302 finally
303 {
304
305 if ( ( _connectionService != null ) && !_bTransactionnal )
306 {
307 _connectionService.freeConnection( _connection );
308 _connectionService = null;
309 }
310
311 _bReleased = true;
312 }
313 }
314
315
316
317
318
319
320
321 @Deprecated
322 public boolean first( )
323 {
324 try
325 {
326 return _resultSet.first( );
327 }
328 catch ( SQLException e )
329 {
330 free( );
331 throw new AppException( getErrorMessage( e ), e );
332 }
333 }
334
335
336
337
338
339 public boolean isLast( )
340 {
341 try
342 {
343 return _resultSet.isLast( );
344 }
345 catch ( SQLException e )
346 {
347 free( );
348 throw new AppException( getErrorMessage( e ), e );
349 }
350 }
351
352
353
354
355
356
357
358 public void setDate( int nIndex, Date date )
359 {
360 try
361 {
362 _statement.setDate( nIndex, date );
363 }
364 catch ( SQLException e )
365 {
366 free( );
367 throw new AppException( getErrorMessage( e ), e );
368 }
369 }
370
371
372
373
374
375
376
377 public void setTime( int nIndex, Time time )
378 {
379 try
380 {
381 _statement.setTime( nIndex, time );
382 }
383 catch ( SQLException e )
384 {
385 free( );
386 throw new AppException( getErrorMessage( e ), e );
387 }
388 }
389
390
391
392
393
394
395
396
397 public void setBinaryStream( int nIndex, InputStream iStream, int nBlength )
398 {
399 try
400 {
401 _statement.setBinaryStream( nIndex, iStream, nBlength );
402 }
403 catch ( SQLException e )
404 {
405 free( );
406 throw new AppException( getErrorMessage( e ), e );
407 }
408 }
409
410
411
412
413
414
415
416 public InputStream getBinaryStream( int nIndex )
417 {
418 try
419 {
420 return _resultSet.getBinaryStream( nIndex );
421 }
422 catch ( SQLException e )
423 {
424 free( );
425 throw new AppException( getErrorMessage( e ), e );
426 }
427 }
428
429
430
431
432
433
434
435 public Blob getBlob( int nIndex )
436 {
437 try
438 {
439 return _resultSet.getBlob( nIndex );
440 }
441 catch ( SQLException e )
442 {
443 free( );
444 throw new AppException( getErrorMessage( e ), e );
445 }
446 }
447
448
449
450
451
452
453
454
455 public Blob getBlob( String strColumnName )
456 {
457 try
458 {
459 return _resultSet.getBlob( strColumnName );
460 }
461 catch ( SQLException e )
462 {
463 free( );
464 throw new AppException( getErrorMessage( e ), e );
465 }
466 }
467
468
469
470
471
472
473
474
475 public byte[] getBytes( int nIndex )
476 {
477 try
478 {
479 return _resultSet.getBytes( nIndex );
480 }
481 catch ( SQLException e )
482 {
483 free( );
484 throw new AppException( getErrorMessage( e ), e );
485 }
486 }
487
488
489
490
491
492
493
494
495 public byte[] getBytes( String strColumnName )
496 {
497 try
498 {
499 return _resultSet.getBytes( strColumnName );
500 }
501 catch ( SQLException e )
502 {
503 free( );
504 throw new AppException( getErrorMessage( e ), e );
505 }
506 }
507
508
509
510
511
512
513
514 public void setInt( int nIndex, int nValue )
515 {
516 try
517 {
518 _statement.setInt( nIndex, nValue );
519
520 if ( _logger.isDebugEnabled( ) )
521 {
522 logParameter( nIndex, nValue );
523 }
524 }
525 catch ( SQLException e )
526 {
527 free( );
528 throw new AppException( getErrorMessage( e ), e );
529 }
530 }
531
532
533
534
535
536
537
538 public void setBoolean( int nIndex, boolean bValue )
539 {
540 try
541 {
542
543 _statement.setInt( nIndex, ( bValue ) ? 1 : 0 );
544 }
545 catch ( SQLException e )
546 {
547 free( );
548 throw new AppException( getErrorMessage( e ), e );
549 }
550 }
551
552
553
554
555
556
557
558 public void setBytes( int nIndex, byte[] tbValue )
559 {
560 try
561 {
562 _statement.setBytes( nIndex, tbValue );
563 }
564 catch ( SQLException e )
565 {
566 free( );
567 throw new AppException( getErrorMessage( e ), e );
568 }
569 }
570
571
572
573
574
575
576
577 public void setString( int nIndex, String strValue )
578 {
579 try
580 {
581 _statement.setString( nIndex, strValue );
582
583 if ( _logger.isDebugEnabled( ) )
584 {
585 logParameter( nIndex, strValue );
586 }
587 }
588 catch ( SQLException e )
589 {
590 free( );
591 throw new AppException( getErrorMessage( e ), e );
592 }
593 }
594
595
596
597
598
599
600
601 public void setTimestamp( int nIndex, Timestamp ts )
602 {
603 try
604 {
605 _statement.setTimestamp( nIndex, ts );
606 }
607 catch ( SQLException e )
608 {
609 free( );
610 throw new AppException( getErrorMessage( e ), e );
611 }
612 }
613
614
615
616
617
618
619 public void setDouble( int nIndex, double dValue )
620 {
621 try
622 {
623 _statement.setDouble( nIndex, dValue );
624 }
625 catch ( SQLException e )
626 {
627 free( );
628 throw new AppException( getErrorMessage( e ), e );
629 }
630 }
631
632
633
634
635
636 public void setDoubleNull( int nIndex )
637 {
638 try
639 {
640 _statement.setNull( nIndex, Types.DOUBLE );
641 }
642 catch ( SQLException e )
643 {
644 free( );
645 throw new AppException( getErrorMessage( e ), e );
646 }
647 }
648
649
650
651
652
653
654
655
656
657
658
659 public Date getDate( int nIndex )
660 {
661 try
662 {
663 return _resultSet.getDate( nIndex );
664 }
665 catch ( SQLException e )
666 {
667 free( );
668 throw new AppException( getErrorMessage( e ), e );
669 }
670 }
671
672
673
674
675
676
677
678
679
680
681
682 public Date getDate( String strColumnName )
683 {
684 try
685 {
686 return _resultSet.getDate( strColumnName );
687 }
688 catch ( SQLException e )
689 {
690 free( );
691 throw new AppException( getErrorMessage( e ), e );
692 }
693 }
694
695
696
697
698
699
700
701
702
703
704
705 public Time getTime( int nIndex )
706 {
707 try
708 {
709 return _resultSet.getTime( nIndex );
710 }
711 catch ( SQLException e )
712 {
713 free( );
714 throw new AppException( getErrorMessage( e ), e );
715 }
716 }
717
718
719
720
721
722
723
724
725
726
727
728 public Time getTime( String strColumnName )
729 {
730 try
731 {
732 return _resultSet.getTime( strColumnName );
733 }
734 catch ( SQLException e )
735 {
736 free( );
737 throw new AppException( getErrorMessage( e ), e );
738 }
739 }
740
741
742
743
744
745
746
747
748
749
750
751 public int getInt( int nIndex )
752 {
753 try
754 {
755 return _resultSet.getInt( nIndex );
756 }
757 catch ( SQLException e )
758 {
759 free( );
760 throw new AppException( getErrorMessage( e ), e );
761 }
762 }
763
764
765
766
767
768
769
770
771
772
773
774 public int getInt( String strColumnName )
775 {
776 try
777 {
778 return _resultSet.getInt( strColumnName );
779 }
780 catch ( SQLException e )
781 {
782 free( );
783 throw new AppException( getErrorMessage( e ), e );
784 }
785 }
786
787
788
789
790
791
792
793
794
795
796
797 public boolean getBoolean( int nIndex )
798 {
799 try
800 {
801 return ( _resultSet.getInt( nIndex ) == 0 ) ? false : true;
802 }
803 catch ( SQLException e )
804 {
805 free( );
806 throw new AppException( getErrorMessage( e ), e );
807 }
808 }
809
810
811
812
813
814
815
816
817
818
819
820 public boolean getBoolean( String strColumnName )
821 {
822 try
823 {
824 return ( _resultSet.getInt( strColumnName ) == 0 ) ? false : true;
825 }
826 catch ( SQLException e )
827 {
828 free( );
829 throw new AppException( getErrorMessage( e ), e );
830 }
831 }
832
833
834
835
836
837
838
839
840
841
842
843 public String getString( int nIndex )
844 {
845 try
846 {
847 return _resultSet.getString( nIndex );
848 }
849 catch ( SQLException e )
850 {
851 free( );
852 throw new AppException( getErrorMessage( e ), e );
853 }
854 }
855
856
857
858
859
860
861
862
863
864
865
866 public String getString( String strColumnName )
867 {
868 try
869 {
870 return _resultSet.getString( strColumnName );
871 }
872 catch ( SQLException e )
873 {
874 free( );
875 throw new AppException( getErrorMessage( e ), e );
876 }
877 }
878
879
880
881
882
883
884
885
886
887
888
889 public Timestamp getTimestamp( int nIndex )
890 {
891 try
892 {
893 return _resultSet.getTimestamp( nIndex );
894 }
895 catch ( SQLException e )
896 {
897 free( );
898 throw new AppException( getErrorMessage( e ), e );
899 }
900 }
901
902
903
904
905
906
907
908
909
910
911
912 public Timestamp getTimestamp( String strColumnName )
913 {
914 try
915 {
916 return _resultSet.getTimestamp( strColumnName );
917 }
918 catch ( SQLException e )
919 {
920 free( );
921 throw new AppException( getErrorMessage( e ), e );
922 }
923 }
924
925
926
927
928
929
930
931
932
933 public double getDouble( String strColumnName )
934 {
935 try
936 {
937 return _resultSet.getDouble( strColumnName );
938 }
939 catch ( SQLException e )
940 {
941 free( );
942 throw new AppException( getErrorMessage( e ), e );
943 }
944 }
945
946
947
948
949
950
951
952
953
954
955 public double getDouble( int nIndex )
956 {
957 try
958 {
959 return _resultSet.getDouble( nIndex );
960 }
961 catch ( SQLException e )
962 {
963 free( );
964 throw new AppException( getErrorMessage( e ), e );
965 }
966 }
967
968
969
970
971
972
973
974
975
976
977
978 public Object getObject( int nIndex )
979 {
980 try
981 {
982 return _resultSet.getObject( nIndex );
983 }
984 catch ( SQLException e )
985 {
986 free( );
987 throw new AppException( getErrorMessage( e ), e );
988 }
989 }
990
991
992
993
994
995 public ResultSet getResultSet( )
996 {
997 return _resultSet;
998 }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 public Object getObject( String strColumnName )
1011 {
1012 try
1013 {
1014 return _resultSet.getObject( strColumnName );
1015 }
1016 catch ( SQLException e )
1017 {
1018 free( );
1019 throw new AppException( getErrorMessage( e ), e );
1020 }
1021 }
1022
1023
1024
1025
1026
1027
1028
1029 public boolean next( )
1030 {
1031 try
1032 {
1033 return _resultSet.next( );
1034 }
1035 catch ( SQLException e )
1036 {
1037 free( );
1038 throw new AppException( getErrorMessage( e ), e );
1039 }
1040 }
1041
1042
1043
1044
1045
1046 public void setIntNull( int nIndex )
1047 {
1048 try
1049 {
1050 _statement.setNull( nIndex, Types.INTEGER );
1051 }
1052 catch ( SQLException e )
1053 {
1054 free( );
1055 throw new AppException( getErrorMessage( e ), e );
1056 }
1057 }
1058
1059
1060
1061
1062
1063 public void setLongNull( int nIndex )
1064 {
1065 try
1066 {
1067 _statement.setNull( nIndex, Types.BIGINT );
1068 }
1069 catch ( SQLException e )
1070 {
1071 free( );
1072 throw new AppException( getErrorMessage( e ), e );
1073 }
1074 }
1075
1076
1077
1078
1079
1080
1081 public void setLong( int nIndex, long lValue )
1082 {
1083 try
1084 {
1085 _statement.setLong( nIndex, lValue );
1086 }
1087 catch ( SQLException e )
1088 {
1089 free( );
1090 throw new AppException( getErrorMessage( e ), e );
1091 }
1092 }
1093
1094
1095
1096
1097
1098
1099 public long getLong( int nIndex )
1100 {
1101 try
1102 {
1103 return _resultSet.getLong( nIndex );
1104 }
1105 catch ( SQLException e )
1106 {
1107 free( );
1108 throw new AppException( getErrorMessage( e ), e );
1109 }
1110 }
1111
1112
1113
1114
1115
1116
1117 public long getLong( String strColumnName )
1118 {
1119 try
1120 {
1121 return _resultSet.getLong( strColumnName );
1122 }
1123 catch ( SQLException e )
1124 {
1125 free( );
1126 throw new AppException( getErrorMessage( e ), e );
1127 }
1128 }
1129
1130
1131
1132
1133 @Override
1134 protected void finalize( ) throws Throwable
1135 {
1136 if ( !_bReleased )
1137 {
1138 free( );
1139 AppLogService.error(
1140 "A call to DAOUtil.free() seems to be missing or an unexpected exception has occured during the use of a DAOUtil object - plugin : " +
1141 _strPluginName + " - SQL statement : " + _strSQL );
1142 }
1143
1144 super.finalize( );
1145 }
1146 }