使用testng多线程来测试成交编号重复的问题

时间:2024-01-04 16:42:50

1.首先编写一个测试用的 function

 CREATE OR REPLACE FUNCTION getDealmainNo_test(dealdate        IN varchar2,
productcodename in varchar2)
/***********************************************************
* linc 2018-01-09
* 方法:获取序列编号
* 入参:成交日期,产品代码
* 返回:序列编号
***********************************************************/
RETURN varchar2 AS
today varchar2(10);
seqno integer;
begindate varchar2(20) := dealdate || ' 00:00:00';
enddate varchar2(20) := dealdate || ' 23:59:59';
seqnoname varchar2(30);
pragma autonomous_transaction;
BEGIN
seqno := 0;
select to_char(sysdate, 'yyyy-mm-dd') into today from dual;
if today = dealdate then
execute immediate 'select seq_' || productcodename ||
'_no.nextval from dual'
into seqno;
else
begin
select nvl(max(to_number(substr(s.tno, -6))), 0) + 1
into seqno
from (select s.tno
from table_A s
where s.dealdate between
to_date(begindate, 'yyyy-mm-dd hh24:mi:ss') and
to_date(enddate, 'yyyy-mm-dd hh24:mi:ss')
and s.tcode = productcodename) s ; exception
when others then
return '';
end;
end if;
seqnoname := productcodename || replace(dealdate, '-', '') ||
lpad(seqno, 6, '0');
RETURN seqnoname;
END getDealmainNo_test;

2.使用java对func进行调用 每次获得一个最大的成交编号 ;

 package main.java.javaprictice;

 import main.java.C3P0XmlSimplify;
import org.testng.annotations.Test; import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by linbo.yang on 2018/1/9.
*/
public class CallOracleFunc {
Connection conn = null;
CallableStatement callableStatement = null;
PreparedStatement ps = null;
ResultSet rs; public Integer save(String no) { Integer rowCount=null ;
String sql="insert into table_A(dealdate,tno,tdate,tcode) values(?,?,?,?) ";
conn = C3P0XmlSimplify.getInstance().getConnection();
//ddate
SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd HH24:mm:ss");
String date = df.format(new Date());
Timestamp timestamp = Timestamp.valueOf(date);
//dealdate
SimpleDateFormat df1 =new SimpleDateFormat("yyyy-MM-dd");
String dealdate = df1.format(new Date()); try {
ps = conn.prepareStatement(sql);
ps.setDate(1,java.sql.Date.valueOf("2018-01-08"));
ps.setString(2,no);
ps.setTimestamp(3,timestamp);
ps.setString(4,"IBO");
rowCount= ps.executeUpdate();
System.out.println("rowCount"+rowCount);
} catch (SQLException e) {
e.printStackTrace();
}finally {
C3P0XmlSimplify.releaseSources(conn);
return rowCount ;
} } public String getDealmainNo() {
String string = null;
try { conn = C3P0XmlSimplify.getInstance().getConnection();
/* ?=call getDealmainNo(?,?);
* 第一个问号(?) 代表输入值,但要制定输入类型:callableStatement.registerOutParameter(1, Types.VARCHAR);
* 第二个问号(?) 代表输入值,通过callableStatement.setString()方法类制定入参的类型 ;
* 。。。。。。。
* */
callableStatement = conn.prepareCall("{?=call getDealmainNo_test(?,?)}");
callableStatement.registerOutParameter(1, Types.VARCHAR);
callableStatement.setString(2, "2018-01-08");
callableStatement.setString(3, "IBO");
callableStatement.execute();
string = callableStatement.getString(1);
System.out.println(string); } catch (Exception e) {
e.printStackTrace();
} finally {
C3P0XmlSimplify.releaseSources(conn);
return string;
}
}
@Test(threadPoolSize = 3,invocationCount = 10,timeOut = 100000)
public void getDealmainNo_() {
CallOracleFunc c=new CallOracleFunc();
String dealmainNo = c.getDealmainNo();
Integer row = c.save(dealmainNo);
System.out.println("dealmainNo="+dealmainNo+" ;row="+row); } public static void main(String[] args) {
CallOracleFunc c=new CallOracleFunc();
String dealmainNo = c.getDealmainNo();
Integer row = c.save(dealmainNo);
System.out.println("dealmainNo="+dealmainNo+" ;row="+row); } }

--数据的相关脚本

    create table table_A(
tid number ,
dealdate date ,
tcode varchar2(100),
tno varchar2(100),
tdate timestamp
) ;
drop table table_A ;
select * from table_A t where t.tid is null ;
delete table_A t where t.tid is null ;
insert into table_A
select v.ID,
v.DEALDATE,
v.PRODUCTCODE,
v.NO,
v.CREATETIME
from v_product_deal_main v ;
insert into table_A(tno,tdate) values(?,?) ; select nvl(max(to_number(substr(s.tno, -6))), 0) + 1
from (select s.tno
from table_A s
where s.dealdate between
to_date('2018-01-08 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
to_date('2018-01-08 23:00:00', 'yyyy-mm-dd hh24:mi:ss')
and s.tcode = 'IBO') s