56,864
社区成员




Delimiter ;
Drop Procedure If Exists `spTest1`;
Delimiter ;;
Create Procedure `spTest1`(
p_DeviceId Int Unsigned
) Begin
If Exists(Select * From tbDevice Where Id = 1) Then
Select Now(); -- 因为 if 语句里必须要有至少一条语句,这里随便执行一条语句
End If;
End ;;
Delimiter;
Declare v_Cnt Int Unsigned;
Select Count(Id) Into v_cnt From tbDevice Where Id = p_DeviceId;
If v_Cnt Is Not Null And v_Cnt > 0 Then
-- ......
End If;
这样写没有问题
其实问题也不大,是因为有一个程序员没有及时提交事务引起的,而正常逻辑是事务必须及时提交,但就是对这种情况比较奇怪而已,毕竟没有对数据表进行过写操作,居然就给锁了mysql> select * from information_schema.INNODB_TRX \G;
*************************** 1. row ***************************
trx_id: 23813821
trx_state: LOCK WAIT
trx_started: 2017-07-03 08:59:34
trx_requested_lock_id: 23813821:19567:4:315
trx_wait_started: 2017-07-03 08:59:34
trx_weight: 2
trx_mysql_thread_id: 93129
trx_query: Update `tbDevice` Set `DeviceSn` = '998' Where `Id` = 1
trx_operation_state: starting index read
trx_tables_in_use: 1
trx_tables_locked: 1
trx_lock_structs: 2
trx_lock_memory_bytes: 360
trx_rows_locked: 1
trx_rows_modified: 0
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 2. row ***************************
trx_id: 23813820
trx_state: RUNNING
trx_started: 2017-07-03 08:59:06
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 2
trx_mysql_thread_id: 93145
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 2
trx_lock_memory_bytes: 360
trx_rows_locked: 1
trx_rows_modified: 0
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
trx_is_read_only: 0
trx_autocommit_non_locking: 0
2 rows in set (0.00 sec)
ERROR:
No query specified
mysql> select * from information_schema.INNODB_LOCKS \G;
*************************** 1. row ***************************
lock_id: 23813821:19567:4:315
lock_trx_id: 23813821
lock_mode: X
lock_type: RECORD
lock_table: `DSPM_Test`.`tbDevice`
lock_index: PRIMARY
lock_space: 19567
lock_page: 4
lock_rec: 315
lock_data: 1
*************************** 2. row ***************************
lock_id: 23813820:19567:4:315
lock_trx_id: 23813820
lock_mode: S
lock_type: RECORD
lock_table: `DSPM_Test`.`tbDevice`
lock_index: PRIMARY
lock_space: 19567
lock_page: 4
lock_rec: 315
lock_data: 1
2 rows in set (0.00 sec)
ERROR:
No query specified
public class Test {
public static void main(String[] args) throws Exception {
Connection conn = null;
CallableStatement cmd = null;
ResultSet rs = null;
String url = "jdbc:mysql://192.168.162.30:3306/DSPM_Test?autoReconnect=true&useUnicode=true&characterEncoding=UTF8";
String username = "root";
String password = "East300376";
Reader reader = null;
BufferedReader breader = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
cmd = conn.prepareCall("{call spTest1(?)}");
cmd.setInt(1, 1);
cmd.execute();
rs = cmd.getResultSet();
if(rs != null && rs.next()){
ResultSetMetaData meta = rs.getMetaData();
for(int i=1; i<=meta.getColumnCount(); i++){
System.out.println(meta.getColumnLabel(i) + "\t" + rs.getObject(i));
}
}
reader = new InputStreamReader(System.in);
breader = new BufferedReader(reader);
System.out.println("Press Enter to continue.");
breader.readLine();
conn.commit();
System.out.println("Press Enter to continue.");
breader.readLine();
}catch(Exception ex){
ex.printStackTrace();
}finally{
DisposeUtil.safeClose(rs);
DisposeUtil.safeClose(cmd);
DisposeUtil.safeClose(conn);
}
}
}
mysql>
mysql> select * from information_schema.INNODB_LOCKS \G;
Empty set (0.00 sec)
ERROR:
No query specified
mysql> select * from information_schema.INNODB_TRX \G;
*************************** 1. row ***************************
trx_id: 23786741
trx_state: RUNNING
trx_started: 2017-06-29 16:11:31
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 2
trx_mysql_thread_id: 87505
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 2
trx_lock_memory_bytes: 360
trx_rows_locked: 1
trx_rows_modified: 0
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
trx_is_read_only: 0
trx_autocommit_non_locking: 0
1 row in set (0.00 sec)
ERROR:
No query specified
从系统表上看,并没有锁表啊