1 /*
2 * Copyright (c) 2002-2025, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.util.sql;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37
38 import org.apache.commons.lang3.NotImplementedException;
39
40 import java.sql.Connection;
41 import java.sql.PreparedStatement;
42 import java.sql.SQLException;
43
44 /**
45 * Class to represents transactions that are shared between plugins. Such transactions will be shared by each plugin using the same pool than the one that
46 * created the transaction.
47 */
48 public class MultiPluginTransaction extends Transaction
49 {
50 private int _nNbTransactionsOpened;
51
52 /**
53 * Constructor
54 */
55 public MultiPluginTransaction( )
56 {
57 super( );
58 _nNbTransactionsOpened = 1;
59 }
60
61 /**
62 * Constructor
63 *
64 * @param plugin
65 * The plugin owner of the transaction
66 */
67 public MultiPluginTransaction( Plugin plugin )
68 {
69 super( plugin );
70 _nNbTransactionsOpened = 1;
71 }
72
73 /**
74 * Get the number of transactions that plugins tried to open simultaneously on the same pool. If the number if 1, then the transaction can be safely
75 * committed. If it is more than 1, then this number should be decremented and the transaction should not be committed.
76 *
77 * @return The number of transactions that was required
78 */
79 public int getNbTransactionsOpened( )
80 {
81 return _nNbTransactionsOpened;
82 }
83
84 /**
85 * Increment the number of transactions by one. See {@link #getNbTransactionsOpened()} for more details.
86 */
87 public void incrementNbTransactionsOpened( )
88 {
89 _nNbTransactionsOpened++;
90 }
91
92 /**
93 * Decrement the number of transactions by one. See {@link #getNbTransactionsOpened()} for more details.
94 */
95 public void decrementNbTransactionsOpened( )
96 {
97 _nNbTransactionsOpened--;
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public PreparedStatement prepareStatement( String strSQL, boolean bLogQueries ) throws SQLException
105 {
106 return prepareStatement( strSQL, null, bLogQueries );
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override
113 public PreparedStatement prepareStatement( String strSQL ) throws SQLException
114 {
115 return prepareStatement( strSQL, null, true );
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 @Override
122 public PreparedStatement prepareStatement( String strSQL, Integer autoGeneratedKeys ) throws SQLException
123 {
124 return prepareStatement( strSQL, autoGeneratedKeys, true );
125 }
126
127 /**
128 * {@inheritDoc}
129 */
130 @Override
131 public PreparedStatement prepareStatement( String strSQL, Integer autoGeneratedKeys, boolean bLogQueries ) throws SQLException
132 {
133 // Get a new statement
134 if ( getConnection( ) == null )
135 {
136 throw new SQLException( "MultiPluginTransaction - Connection has been closed. The new prepared statement can not be created : "
137 + ( bLogQueries ? strSQL : "(query log disabled)" ) );
138 }
139
140 Connection connection = getConnection( );
141 if ( autoGeneratedKeys != null )
142 {
143 return connection.prepareStatement( strSQL, autoGeneratedKeys );
144 }
145 else
146 {
147 return connection.prepareStatement( strSQL );
148 }
149 }
150
151 /**
152 * {@inheritDoc}
153 */
154 @Override
155 public void executeStatement( ) throws SQLException
156 {
157 throw new NotImplementedException(
158 "The method executeStatement( ) of class MultiPluginTransaction must not be called. Use manually PreparedStatement.executeQuery() instead." );
159 }
160
161 /**
162 * {@inheritDoc}
163 */
164 @Override
165 public void commit( )
166 {
167 _nNbTransactionsOpened = 0;
168 super.commit( );
169 }
170
171 /**
172 * {@inheritDoc}
173 */
174 @Override
175 public void rollback( Exception e )
176 {
177 _nNbTransactionsOpened = 0;
178 super.rollback( e );
179 }
180 }