55import com .codingapi .tx .client .spi .transaction .txc .resource .def .TxcSqlExecutor ;
66import com .codingapi .tx .client .spi .transaction .txc .resource .def .bean .*;
77import com .codingapi .tx .client .spi .transaction .txc .resource .util .SqlUtils ;
8+ import com .codingapi .tx .commons .exception .TxcLogicException ;
89import lombok .extern .slf4j .Slf4j ;
910import org .springframework .beans .factory .annotation .Autowired ;
1011import org .springframework .stereotype .Service ;
1112import org .springframework .util .DigestUtils ;
1213
1314import java .nio .charset .StandardCharsets ;
15+ import java .sql .Connection ;
1416import java .sql .SQLException ;
1517import java .util .List ;
1618import java .util .Map ;
@@ -29,53 +31,50 @@ public class TxcServiceImpl implements TxcService {
2931 private TxcSqlExecutor txcSqlExecutor ;
3032
3133 @ Override
32- public void lockResource (LockInfo lockInfo , RollbackInfo rollbackInfo ) throws SQLException {
34+ public void lockResource (LockInfo lockInfo , RollbackInfo rollbackInfo ) throws TxcLogicException {
3335 try {
34- DTXLocal .makeUnProxy ();
36+ Connection connection = ( Connection ) DTXLocal .cur (). getResource ();
3537 // key value MD5 HEX to store
3638 lockInfo .setKeyValue (DigestUtils .md5DigestAsHex (lockInfo .getKeyValue ().getBytes (StandardCharsets .UTF_8 )));
37- txcSqlExecutor .tryLock (lockInfo );
39+ txcSqlExecutor .tryLock (connection , lockInfo );
3840 } catch (SQLException e ) {
3941 rollbackInfo .setStatus (-1 );
40- throw new SQLException ("Resource is locked! Place try again later." );
41- } finally {
42- DTXLocal .undoProxyStatus ();
42+ throw new TxcLogicException ("Resource is locked! Place try again later." );
4343 }
4444 }
4545
4646 @ Override
47- public void lockSelect (SelectImageParams selectImageParams , boolean isxLock ) throws SQLException {
48- List < ModifiedRecord > modifiedRecords ;
47+ public void lockSelect (SelectImageParams selectImageParams , boolean isxLock ) throws TxcLogicException {
48+ Connection connection = ( Connection ) DTXLocal . cur (). getResource () ;
4949 try {
50- DTXLocal .makeUnProxy ();
51- modifiedRecords = txcSqlExecutor .selectSqlPreviousPrimaryKeys (selectImageParams );
52- } finally {
53- DTXLocal .undoProxyStatus ();
54- }
55- for (ModifiedRecord modifiedRecord : modifiedRecords ) {
56- for (Map .Entry <String , FieldCluster > entry : modifiedRecord .getFieldClusters ().entrySet ()) {
57- String k = entry .getKey ();
58- FieldCluster v = entry .getValue ();
59- lockResource (new LockInfo ()
60- .setGroupId (selectImageParams .getGroupId ())
61- .setUnitId (selectImageParams .getUnitId ())
62- .setxLock (isxLock )
63- .setKeyValue (v .getPrimaryKeys ().toString ())
64- .setTableName (k ), selectImageParams .getRollbackInfo ());
50+ List <ModifiedRecord > modifiedRecords = txcSqlExecutor .selectSqlPreviousPrimaryKeys (connection , selectImageParams );
51+ for (ModifiedRecord modifiedRecord : modifiedRecords ) {
52+ for (Map .Entry <String , FieldCluster > entry : modifiedRecord .getFieldClusters ().entrySet ()) {
53+ String k = entry .getKey ();
54+ FieldCluster v = entry .getValue ();
55+ lockResource (new LockInfo ()
56+ .setGroupId (selectImageParams .getGroupId ())
57+ .setUnitId (selectImageParams .getUnitId ())
58+ .setxLock (isxLock )
59+ .setKeyValue (v .getPrimaryKeys ().toString ())
60+ .setTableName (k ), selectImageParams .getRollbackInfo ());
61+ }
6562 }
63+ } catch (SQLException e ) {
64+ throw new TxcLogicException (e );
6665 }
6766 }
6867
6968 @ Override
70- public void resolveUpdateImage (UpdateImageParams updateImageParams ) throws SQLException {
69+ public void resolveUpdateImage (UpdateImageParams updateImageParams ) throws TxcLogicException {
7170
7271 // 前置镜像数据集
7372 List <ModifiedRecord > modifiedRecords ;
73+ Connection connection = (Connection ) DTXLocal .cur ().getResource ();
7474 try {
75- DTXLocal .makeUnProxy ();
76- modifiedRecords = txcSqlExecutor .updateSqlPreviousData (updateImageParams );
77- } finally {
78- DTXLocal .undoProxyStatus ();
75+ modifiedRecords = txcSqlExecutor .updateSqlPreviousData (connection , updateImageParams );
76+ } catch (SQLException e ) {
77+ throw new TxcLogicException (e );
7978 }
8079
8180
@@ -115,25 +114,25 @@ public void resolveUpdateImage(UpdateImageParams updateImageParams) throws SQLEx
115114 // Lock Resource
116115 this .lockResource (new LockInfo ()
117116 .setxLock (true )
117+ .setKeyValue (v .getPrimaryKeys ().toString ())
118118 .setGroupId (updateImageParams .getGroupId ())
119119 .setUnitId (updateImageParams .getUnitId ())
120- .setKeyValue (v .getPrimaryKeys ().toString ())
121120 .setTableName (k ), updateImageParams .getRollbackInfo ());
122121 }
123122 }
124123 log .debug ("rollback info: {}" , updateImageParams .getRollbackInfo ());
125124 }
126125
127126 @ Override
128- public void resolveDeleteImage (DeleteImageParams deleteImageParams ) throws SQLException {
127+ public void resolveDeleteImage (DeleteImageParams deleteImageParams ) throws TxcLogicException {
129128
130129 // 前置数据
131130 List <ModifiedRecord > modifiedRecords ;
131+ Connection connection = (Connection ) DTXLocal .cur ().getResource ();
132132 try {
133- DTXLocal .makeUnProxy ();
134- modifiedRecords = txcSqlExecutor .deleteSqlPreviousData (deleteImageParams );
135- } finally {
136- DTXLocal .undoProxyStatus ();
133+ modifiedRecords = txcSqlExecutor .deleteSqlPreviousData (connection , deleteImageParams );
134+ } catch (SQLException e ) {
135+ throw new TxcLogicException (e );
137136 }
138137
139138 // rollback sql
@@ -168,7 +167,7 @@ public void resolveDeleteImage(DeleteImageParams deleteImageParams) throws SQLEx
168167 }
169168
170169 @ Override
171- public void writeUndoLog (String groupId , String unitId , RollbackInfo rollbackInfo ) {
170+ public void writeUndoLog (String groupId , String unitId , RollbackInfo rollbackInfo ) throws TxcLogicException {
172171 if (rollbackInfo .getRollbackSqlList ().size () == 0 ) {
173172 return ;
174173 }
@@ -182,21 +181,21 @@ public void writeUndoLog(String groupId, String unitId, RollbackInfo rollbackInf
182181 DTXLocal .makeUnProxy ();
183182 txcSqlExecutor .writeUndoLog (undoLogDO );
184183 } catch (SQLException e ) {
185- log . error ( "error: {} code: {}" , e . getMessage (), e . getErrorCode () );
184+ throw new TxcLogicException ( e );
186185 } finally {
187186 DTXLocal .undoProxyStatus ();
188187 }
189188 }
190189
191190 @ Override
192- public void cleanTxc (String groupId , String unitId ) throws SQLException {
191+ public void cleanTxc (String groupId , String unitId ) throws TxcLogicException {
193192 // 清理事务单元相关锁
194193 try {
195194 DTXLocal .makeUnProxy ();
196195 txcSqlExecutor .clearLock (groupId , unitId );
197196 } catch (SQLException e ) {
198197 if (e .getErrorCode () != SqlUtils .MYSQL_TABLE_NOT_EXISTS_CODE ) {
199- throw e ;
198+ throw new TxcLogicException ( e ) ;
200199 }
201200 } finally {
202201 DTXLocal .undoProxyStatus ();
@@ -208,18 +207,20 @@ public void cleanTxc(String groupId, String unitId) throws SQLException {
208207 txcSqlExecutor .clearUndoLog (groupId , unitId );
209208 } catch (SQLException e ) {
210209 if (e .getErrorCode () != SqlUtils .MYSQL_TABLE_NOT_EXISTS_CODE ) {
211- throw e ;
210+ throw new TxcLogicException ( e ) ;
212211 }
213212 } finally {
214213 DTXLocal .undoProxyStatus ();
215214 }
216215 }
217216
218217 @ Override
219- public void undo (String groupId , String unitId ) throws SQLException {
218+ public void undo (String groupId , String unitId ) throws TxcLogicException {
220219 try {
221220 DTXLocal .makeUnProxy ();
222221 txcSqlExecutor .applyUndoLog (groupId , unitId );
222+ } catch (SQLException e ) {
223+ throw new TxcLogicException (e );
223224 } finally {
224225 DTXLocal .undoProxyStatus ();
225226 }
0 commit comments