NutzCN Logo
问答 nutz的dao无法拿到SELECT SCOPE_IDENTITY()返回值
发布于 2805天前 作者 wx_beqqg3fq15cklpbn1is0 2586 次浏览 复制 上一个帖子 下一个帖子
标签: ssdb

数据库是sql server的 现在需要根据SELECT SCOPE_IDENTITY() 获取主键值,类似于
@Id
@Next( @SQL("SELECT SCOPE_IDENTITY()"))
@Column("ApplyerID")这样。 现在发现nutz的dao方法每次增删改查做完都会回收数据库连接,再去重连,日志里是这样的
16:55:57.027 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20000} clearParameters.
16:55:57.027 [main] DEBUG com.alibaba.druid.pool.PreparedStatementPool - {conn-10002, pstmt-20000} enter cache
16:55:57.027 [main] DEBUG druid.sql.Connection - {conn-10002} pool-recycle
16:57:38.780 [main] DEBUG druid.sql.Connection - {conn-10002} pool-connect
16:57:38.780 [main] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT SCOPE_IDENTITY()

请问如何解决?谢谢

38 回复

用的是老版本吧???

org.nutz
nutz
1.r.58 是版本太低了嘛?

包个事务吧

Trans.exec(() -> {
}包了

老大啊 换了 1.r.61还是不行啊 事务原来就包了

因为在这张表上有触发器,所以只能用SELECT SCOPE_IDENTITY()来获取当前会话的主键最大值,另外2个都不行SELECT @@@@IDENTITY 会拿到 触发器里表的ID 不能用, Ident_Current 会有并发问题 。老大 这个问题有解么,为什么每次做完好收回数据库链接重新连?

同一个事务内, 是同一个连接的

Trans.exec(() -> {
try {
SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat("yyyyMMdd");
String token = toString(map.get("token")).toString();
String guarantor = toString(map.get("guaranteeId")).toString();
RetailApplyContract contract = getDao().fetch(RetailApplyContract.class, Cnd.where("token", "=", token));
if (StringsExpand.isEmpty(guarantor)) {
RetailGuarantee retailGuarantee = new RetailGuarantee();
retailGuarantee.setGuarantorType("P");
retailGuarantee.setStatus("Y");
retailGuarantee.setApplyId(contract.getApplyId());
retailGuarantee.setGuaranteeRalation(toString(map.get("guaranteeRalation")).toString());
RetailApplyer applyer = new RetailApplyer();
applyer = packageApplyer(applyer, map);
if (StringsExpand.isNotEmpty(map.get("p_name"))) {
RetailApplyer retailApplyerSpouse = new RetailApplyer();
retailApplyerSpouse = packageApplyerSpouse(retailApplyerSpouse, map);
getDao().insert(retailApplyerSpouse);
applyer.setSpouseID(retailApplyerSpouse.getApplyerId());
}
getDao().insert(applyer);
retailGuarantee.setGuarantor(applyer.getApplyerId());
getDao().insert(retailGuarantee);
returnMap.put("status", "success");
returnMap.put("guarantor", retailGuarantee.getGuarantor());
} else {
RetailApplyer applyer = getDao().fetch(RetailApplyer.class, Cnd.where("applyerId", "=", guarantor));
RetailApplyer retailApplyerSpouse;
applyer = packageApplyer(applyer, map);
if (StringsExpand.isNotEmpty(map.get("p_name"))) {
retailApplyerSpouse = new RetailApplyer();
if (StringsExpand.isNotEmpty(applyer.getSpouseID())) {
retailApplyerSpouse = getDao().fetch(RetailApplyer.class, applyer.getSpouseID());
retailApplyerSpouse = packageApplyerSpouse(retailApplyerSpouse, map);
getDao().update(retailApplyerSpouse);
} else {
retailApplyerSpouse = packageApplyerSpouse(retailApplyerSpouse, map);
getDao().insert(retailApplyerSpouse);
applyer.setSpouseID(retailApplyerSpouse.getApplyerId());
}
}
if (StringsExpand.isNotEmpty(map.get("marriageStatus")) && "Single".equals(map.get("marriageStatus"))) {
retailApplyerSpouse = getDao().fetch(RetailApplyer.class, Cnd.where("applyerId", "=", applyer.getSpouseID()));
if (null != retailApplyerSpouse) {
applyer.setSpouseID(null);
applyer.setMarriageStatus("Single");
getDao().update(applyer);
getDao().delete(retailApplyerSpouse);
}
}
getDao().update(applyer);
returnMap.put("status", "success");
returnMap.put("guarantor", guarantor);
}

        } catch (Exception e) {
            e.printStackTrace();
            log.error("save guarantee failed===" + e.getMessage());
            returnMap.put("status", "failed");
        }
    });

我这么包的事务 不对吗?

代码太多了, 写个简单的方法来测试

Trans.exec(() -> {
getDao().insert(applyer);
retailGuarantee.setGuarantor(applyer.getApplyerId()); //applyer.getApplyerId()就是null
getDao().insert(retailGuarantee);
}

然后对应的druid日志再贴一次, 前后打点日志区分一下

16:55:56.831 [main] DEBUG org.nutz.dao.impl.FileSqlManager - load 9 sql >> all.sql from root=sqls/
16:55:56.831 [main] WARN org.nutz.dao.impl.FileSqlManager - SqlManager regex is Deprecated!! it will be ignore!!
16:55:56.832 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 'dataSource'<>
16:55:56.832 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 'fileManager'<>
16:55:56.836 [main] DEBUG org.nutz.dao.impl.NutDao - NutDao[_selfId=0] init ...
16:55:56.851 [main] INFO org.nutz.filepool.NutFilePool - Init file-pool by: C:\Users\‘/.nutz/tmp/dao/ [200000]
16:55:56.851 [main] DEBUG org.nutz.filepool.NutFilePool - file-pool.home: 'C:\Users\‘.nutz\tmp\dao'
16:55:56.851 [main] INFO org.nutz.filepool.NutFilePool - file-pool.cursor: 0
16:55:56.856 [main] DEBUG org.nutz.dao.jdbc.Jdbcs - Jdbcs init complete
16:55:56.856 [main] INFO org.nutz.dao.jdbc.Jdbcs - Get Connection from DataSource for JdbcExpert, if I lock at here, check your database server and configure
16:55:56.886 [main] ERROR com.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set
16:55:56.955 [main] DEBUG druid.sql.Connection - {conn-10001} connected
16:55:56.965 [main] DEBUG druid.sql.Connection - {conn-10002} connected
16:55:56.970 [main] INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited
16:55:56.972 [main] DEBUG druid.sql.Connection - {conn-10002} pool-connect
16:55:56.975 [main] DEBUG druid.sql.Connection - {conn-10002} pool-recycle
16:55:56.979 [main] DEBUG druid.sql.Connection - {conn-10002} pool-connect
16:55:56.979 [main] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Driver --> 1.2.4
16:55:56.979 [main] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Name --> jTDS Type 4 JDBC Driver for MS SQL Server and Sybase
16:55:56.979 [main] DEBUG org.nutz.dao.impl.DaoSupport - JDBC URL --> jdbc:jtds:sqlserver://10.116.4.32:1433/QCDATA
16:55:56.979 [main] DEBUG druid.sql.Connection - {conn-10002} pool-recycle
16:55:56.979 [main] DEBUG org.nutz.dao.impl.DaoSupport - Database info --> SQLSERVER:[Microsoft SQL Server - 09.00.5000]
16:55:56.981 [main] DEBUG org.nutz.dao.impl.DaoSupport - SqlManager Sql count=9
16:55:56.981 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 'dataSource'<interface javax.sql.DataSource>
16:55:57.009 [main] DEBUG druid.sql.Connection - {conn-10002} pool-connect
16:55:57.009 [main] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - INSERT INTO Retail_Applyer (Name, CertType, CertNo, Sex, Birthday, MarriageStatus, DrivingLicenseHas, DrivingLicenseNo, Education, SpouseID, MailAddress, JobIndustry, JobCompany, JobPosition, JobEntryDate, JobTitle, JobCompIndustry, JobCompProvince, JobCompCity, JobCompPhoneNo, JobCompZip, JobCompAddress, JobCompAreaNo, CurrProvince, CurrCity, CurrEntryDate, CurrAddressZip, CurrLivingCondition, CurrAddress, LastProvince, LastCity, LastEntryDate, LastAddressZip, LastAddress, PhoneNo, CellNo, Email, RegiProvince, RegiCity, RegiPhoneNo, RegiZip, RegiAddress, IncomeYear, IncomeOther, FamilyMembers, FamilyLivingTogether, BankOpen, BankBranch, BankAccount, BankOpenerName, idCardCheckFlg, CusType, MainIncomeFrom, OwnEstate, PropertyOwner, PropertyLoan, PropertyAddType, OtherPropertyAddress, LastUpdBy, LastUpdTime, DesignaterName, DesignaterIDNo, DesignaterMobile, DesignaterPhone, PropertyLocation, PSBCrawlMark, CourtCrawlMark, CreditCrawlMark, token) VALUES ( '潘卫忠', 'IdentityCard', '330225197303217511', 'Male', 19730321, 'Married', 'Has', '330225730321751', 'SeniorMiddleSchool', 2, 'CurrAddr', 'Other', '浙江建安实业集团徐州分公司', 'Other', 20061101, 'None', 'Other', '01050000', '01050128', '85832739', '221000', '徐州市泉山区凤凰山庄商办楼B#-1-206、207、208、301', null, '01050000', '01050128', 19971101, '221000', 'Other', '徐州市西安北路铜沛路交叉口第四监狱', null, null, null, '', ' ', '65761635', '15996969801', '', '02890000', '02890308', '0574-65761635', '315728', '浙江省象山县晓塘乡青脚村', '84000.00', null, 1, null, null, '', '', '潘卫忠', 'Y', null, null, null, null, null, null, null, null, null, null, null, null, null, null, 'Y', null, null, null)
16:55:57.014 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20000} created.
INSERT INTO Retail_Applyer (Name, CertType, CertNo, Sex, Birthday, MarriageStatus, DrivingLicenseHas, DrivingLicenseNo, Education, SpouseID, MailAddress, JobIndustry, JobCompany, JobPosition, JobEntryDate, JobTitle, JobCompIndustry, JobCompProvince, JobCompCity, JobCompPhoneNo, JobCompZip, JobCompAddress, JobCompAreaNo, CurrProvince, CurrCity, CurrEntryDate, CurrAddressZip, CurrLivingCondition, CurrAddress, LastProvince, LastCity, LastEntryDate, LastAddressZip, LastAddress, PhoneNo, CellNo, Email, RegiProvince, RegiCity, RegiPhoneNo, RegiZip, RegiAddress, IncomeYear, IncomeOther, FamilyMembers, FamilyLivingTogether, BankOpen, BankBranch, BankAccount, BankOpenerName, idCardCheckFlg, CusType, MainIncomeFrom, OwnEstate, PropertyOwner, PropertyLoan, PropertyAddType, OtherPropertyAddress, LastUpdBy, LastUpdTime, DesignaterName, DesignaterIDNo, DesignaterMobile, DesignaterPhone, PropertyLocation, PSBCrawlMark, CourtCrawlMark, CreditCrawlMark, token) VALUES ( '潘卫忠', 'IdentityCard', '330225197303217511', 'Male', 19730321, 'Married', 'Has', '330225730321751', 'SeniorMiddleSchool', 2, 'CurrAddr', 'Other', '浙江建安实业集团徐州分公司', 'Other', 20061101, 'None', 'Other', '01050000', '01050128', '85832739', '221000', '徐州市泉山区凤凰山庄商办楼B#-1-206、207、208、301', null, '01050000', '01050128', 19971101, '221000', 'Other', '徐州市西安北路铜沛路交叉口第四监狱', null, null, null, '', ' ', '65761635', '15996969801', '', '02890000', '02890308', '0574-65761635', '315728', '浙江省象山县晓塘乡青脚村', '84000.00', null, 1, null, null, '', '', '潘卫忠', 'Y', null, null, null, null, null, null, null, null, null, null, null, null, null, null, 'Y', null, null, null)
16:55:57.020 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20000} Parameters : []
16:55:57.020 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20000} Types : []
16:55:57.027 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20000} executed.
INSERT INTO Retail_Applyer (Name, CertType, CertNo, Sex, Birthday, MarriageStatus, DrivingLicenseHas, DrivingLicenseNo, Education, SpouseID, MailAddress, JobIndustry, JobCompany, JobPosition, JobEntryDate, JobTitle, JobCompIndustry, JobCompProvince, JobCompCity, JobCompPhoneNo, JobCompZip, JobCompAddress, JobCompAreaNo, CurrProvince, CurrCity, CurrEntryDate, CurrAddressZip, CurrLivingCondition, CurrAddress, LastProvince, LastCity, LastEntryDate, LastAddressZip, LastAddress, PhoneNo, CellNo, Email, RegiProvince, RegiCity, RegiPhoneNo, RegiZip, RegiAddress, IncomeYear, IncomeOther, FamilyMembers, FamilyLivingTogether, BankOpen, BankBranch, BankAccount, BankOpenerName, idCardCheckFlg, CusType, MainIncomeFrom, OwnEstate, PropertyOwner, PropertyLoan, PropertyAddType, OtherPropertyAddress, LastUpdBy, LastUpdTime, DesignaterName, DesignaterIDNo, DesignaterMobile, DesignaterPhone, PropertyLocation, PSBCrawlMark, CourtCrawlMark, CreditCrawlMark, token) VALUES ( '潘卫忠', 'IdentityCard', '330225197303217511', 'Male', 19730321, 'Married', 'Has', '330225730321751', 'SeniorMiddleSchool', 2, 'CurrAddr', 'Other', '浙江建安实业集团徐州分公司', 'Other', 20061101, 'None', 'Other', '01050000', '01050128', '85832739', '221000', '徐州市泉山区凤凰山庄商办楼B#-1-206、207、208、301', null, '01050000', '01050128', 19971101, '221000', 'Other', '徐州市西安北路铜沛路交叉口第四监狱', null, null, null, '', ' ', '65761635', '15996969801', '', '02890000', '02890308', '0574-65761635', '315728', '浙江省象山县晓塘乡青脚村', '84000.00', null, 1, null, null, '', '', '潘卫忠', 'Y', null, null, null, null, null, null, null, null, null, null, null, null, null, null, 'Y', null, null, null)
16:55:57.027 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20000} executed. 10.667687 millis.
INSERT INTO Retail_Applyer (Name, CertType, CertNo, Sex, Birthday, MarriageStatus, DrivingLicenseHas, DrivingLicenseNo, Education, SpouseID, MailAddress, JobIndustry, JobCompany, JobPosition, JobEntryDate, JobTitle, JobCompIndustry, JobCompProvince, JobCompCity, JobCompPhoneNo, JobCompZip, JobCompAddress, JobCompAreaNo, CurrProvince, CurrCity, CurrEntryDate, CurrAddressZip, CurrLivingCondition, CurrAddress, LastProvince, LastCity, LastEntryDate, LastAddressZip, LastAddress, PhoneNo, CellNo, Email, RegiProvince, RegiCity, RegiPhoneNo, RegiZip, RegiAddress, IncomeYear, IncomeOther, FamilyMembers, FamilyLivingTogether, BankOpen, BankBranch, BankAccount, BankOpenerName, idCardCheckFlg, CusType, MainIncomeFrom, OwnEstate, PropertyOwner, PropertyLoan, PropertyAddType, OtherPropertyAddress, LastUpdBy, LastUpdTime, DesignaterName, DesignaterIDNo, DesignaterMobile, DesignaterPhone, PropertyLocation, PSBCrawlMark, CourtCrawlMark, CreditCrawlMark, token) VALUES ( '潘卫忠', 'IdentityCard', '330225197303217511', 'Male', 19730321, 'Married', 'Has', '330225730321751', 'SeniorMiddleSchool', 2, 'CurrAddr', 'Other', '浙江建安实业集团徐州分公司', 'Other', 20061101, 'None', 'Other', '01050000', '01050128', '85832739', '221000', '徐州市泉山区凤凰山庄商办楼B#-1-206、207、208、301', null, '01050000', '01050128', 19971101, '221000', 'Other', '徐州市西安北路铜沛路交叉口第四监狱', null, null, null, '', ' ', '65761635', '15996969801', '', '02890000', '02890308', '0574-65761635', '315728', '浙江省象山县晓塘乡青脚村', '84000.00', null, 1, null, null, '', '', '潘卫忠', 'Y', null, null, null, null, null, null, null, null, null, null, null, null, null, null, 'Y', null, null, null)
16:55:57.027 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20000} clearParameters.
16:55:57.027 [main] DEBUG com.alibaba.druid.pool.PreparedStatementPool - {conn-10002, pstmt-20000} enter cache
16:55:57.027 [main] DEBUG druid.sql.Connection - {conn-10002} pool-recycle
16:57:38.780 [main] DEBUG druid.sql.Connection - {conn-10002} pool-connect
16:57:38.780 [main] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT SCOPE_IDENTITY()
16:57:38.780 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20001} created
16:57:38.784 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20001} executed.
SELECT SCOPE_IDENTITY()
16:57:38.785 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20001, rs-50000} query executed. 4.000447 millis.
SELECT SCOPE_IDENTITY()
16:57:38.785 [main] DEBUG druid.sql.ResultSet - {conn-10002, stmt-20001, rs-50000} open
16:57:38.787 [main] DEBUG druid.sql.ResultSet - {conn-10002, stmt-20001, rs-50000} Header: []
16:57:38.790 [main] DEBUG druid.sql.ResultSet - {conn-10002, stmt-20001, rs-50000} closed
16:57:38.790 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20001} closed
16:57:38.790 [main] DEBUG druid.sql.Connection - {conn-10002} pool-recycle

话说, 你并不需要自己加@Next吧?

难道是你改过什么东西? 感觉不可能的样子, 我写个testcase试试看

我debug了com.alibaba.druid.pool.DruidPooledConnection.close()

仅在事务关闭的时候才调用到了,所以并无问题

谢谢了 ,把@Next去掉就好了, 大牛能说下原因么。

去掉之后,日志有啥变化?贴一下看看

级别:DEBUG 时间:2017-04-20 08:51:30,588 
 位置:org.nutz.dao.impl.sql.run.NutDaoExecutor 113 
 内容:select * from(select row_number()over(order by __tc__)__rn__,* from(select top 1 0 __tc__,    * FROM Retail_ApplyContract  WHERE token=?)t)tt where __rn__ > 0 order by __rn__ 
    |                                1 |
    |----------------------------------|
    | 22d6cb54c5fa5829e7f607f8a3ca4110 |
  For example:> "select * from(select row_number()over(order by __tc__)__rn__,* from(select top 1 0 __tc__,    * FROM Retail_ApplyContract  WHERE token='22d6cb54c5fa5829e7f607f8a3ca4110')t)tt where __rn__ > 0 order by __rn__ "  
-------------------------------------------------------

级别:DEBUG 时间:2017-04-20 08:51:32,612 
 位置:com.alibaba.druid.pool.PreparedStatementPool 123 
 内容:{conn-10002, pstmt-20001} enter cache  
-------------------------------------------------------

级别:DEBUG 时间:2017-04-20 08:51:47,035 
 位置:org.nutz.dao.impl.sql.run.NutDaoExecutor 113 
 内容:INSERT INTO Retail_Applyer(SpouseID,Name,Sex,CertType,CertNo,MarriageStatus,Birthday,Education,MailAddress,DrivingLicenseHas,DesignaterName,DesignaterIDNo,JobIndustry,JobEntryDate,JobCompany,jobPosition,JobTitle,JobCompIndustry,JobCompProvince,JobCompCity,JobCompPhoneNo,JobCompZip,JobCompAddress,JobCompAreaNo,CurrProvince,CurrCity,CurrEntryDate,CurrAddressZip,CurrLivingCondition,PropertyAddType,CurrAddress,OwnEstate,PropertyLocation,PropertyOwner,PropertyLoan,OtherPropertyAddress,PhoneNo,CellNo,Email,RegiProvince,RegiCity,RegiPhoneNo,RegiZip,RegiAddress,IncomeYear,idCardCheckFlg,token) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) 
    |    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 |     35 | 36 |      37 |          38 |              39 |       40 |       41 |          42 |     43 | 44 |    45 | 46 |   47 |
    |------|----|--------|--------------|--------------------|--------|----------|----------|----------|----|--|--|------------|------|----|---------|-------------|----|----------|----------|-------------|--------|----|------|----------|----------|----------|--------|-----------|----------|----|---|-------|--|--------|--|---------|-------------|-----------------|----------|----------|-------------|--------|----|-------|---|------|
    | NULL | 卢娜 | Female | IdentityCard | 320303199305204321 | Single | 19930520 | Bachelor | CurrAddr | No |  |  | Technician | NULL | 上海 | Eployee | Middletitle | IT | 01010000 | 01010825 | 14546458464 | 142156 | 北京 | 0214 | 01010000 | 01010825 | 20170418 | 452361 | RentHouse | CurrAddr | 上海 | N | Local |  | NoLoan |  | 5434544 | 14576444495 | 6273388@163.com | 01010000 | 01010825 | 15464542439 | 124544 | 上海 | 12000 | N | NULL |
  For example:> "INSERT INTO Retail_Applyer(SpouseID,Name,Sex,CertType,CertNo,MarriageStatus,Birthday,Education,MailAddress,DrivingLicenseHas,DesignaterName,DesignaterIDNo,JobIndustry,JobEntryDate,JobCompany,jobPosition,JobTitle,JobCompIndustry,JobCompProvince,JobCompCity,JobCompPhoneNo,JobCompZip,JobCompAddress,JobCompAreaNo,CurrProvince,CurrCity,CurrEntryDate,CurrAddressZip,CurrLivingCondition,PropertyAddType,CurrAddress,OwnEstate,PropertyLocation,PropertyOwner,PropertyLoan,OtherPropertyAddress,PhoneNo,CellNo,Email,RegiProvince,RegiCity,RegiPhoneNo,RegiZip,RegiAddress,IncomeYear,idCardCheckFlg,token) VALUES('NULL','卢娜','Female','IdentityCard','320303199305204321','Single',19930520,'Bachelor','CurrAddr','No','','','Technician','NULL','上海','Eployee','Middletitle','IT','01010000','01010825','14546458464','142156','北京','0214','01010000','01010825',20170418,'452361','RentHouse','CurrAddr','上海','N','Local','','NoLoan','','5434544','14576444495','6273388@163.com','01010000','01010825','15464542439','124544','上海','12000','N','NULL') "  
-------------------------------------------------------

级别:DEBUG 时间:2017-04-20 08:51:54,393 
 位置:com.alibaba.druid.pool.PreparedStatementPool 123 
 内容:{conn-10002, pstmt-20003} enter cache  
-------------------------------------------------------

级别:DEBUG 时间:2017-04-20 08:51:54,397 
 位置:org.nutz.dao.impl.sql.run.NutDaoExecutor 113 
 内容:SELECT @@IDENTITY as ApplyerID  
-------------------------------------------------------

级别:DEBUG 时间:2017-04-20 08:52:05,977 
 位置:org.nutz.dao.impl.sql.run.NutDaoExecutor 113 
 内容:INSERT INTO Retail_Guarantee(applyId,guaranteeRalation,guarantorType,guarantor,status) VALUES(?,?,?,?,?) 
    |       1 |  2 | 3 |       4 | 5 |
    |---------|----|---|---------|---|
    | 2615324 | 姐妹 | P | 6996913 | Y |
  For example:> "INSERT INTO Retail_Guarantee(applyId,guaranteeRalation,guarantorType,guarantor,status) VALUES(2615324,'姐妹','P',6996913,'Y') "  
-------------------------------------------------------

级别:DEBUG 时间:2017-04-20 08:52:06,009 
 位置:com.alibaba.druid.pool.PreparedStatementPool 123 
 内容:{conn-10002, pstmt-20006} enter cache  
-------------------------------------------------------

级别:DEBUG 时间:2017-04-20 08:52:06,011 
 位置:org.nutz.dao.impl.sql.run.NutDaoExecutor 113 
 内容:SELECT @@IDENTITY as GuaranteeId  
-------------------------------------------------------

6996913 就是主键。

没有关连接了?很神奇,你用的nutdao有什么特别设置吗?

晕 老大 还是不行 把@Next去掉 主键的生成方式就变成SELECT @@IDENTITY as ApplyerID 不是SELECT SCOPE_IDENTITY() 我晕死

加上@Next,然后debug一下insert时,是哪里调用了DruidPooledConnection的close方法

数据库版本,druid版本,发一下,我试试重现


com.alibaba
druid
1.0.13

sqlserver 2005, 兼容级别2000

debug了吗?

另外,为啥必须是SCOPE_IDENTITY,为啥@@IDENTITY不能接受呢?

因为这张表上有触发器 用@@IDENTITY 拿到的ID 不是这张表上的主键 结果就是我拿到的主键值 不是真正插入表的主键

要么debug一下close方法查根源, 要么下载源码改org.nutz.dao.impl.jdbc.sqlserver2005.Sqlserver2005JdbcExpert.fetchPojoId(Entity<?>, MappingField)方法吧

DEBUG druid.sql.Statement - {conn-10002, pstmt-20001} executed. 56.308742 millis. 
INSERT INTO Retail_Applyer(SpouseID,Name,Sex,CertType,CertNo,MarriageStatus,Birthday,Education,MailAddress,DrivingLicenseHas,DesignaterName,DesignaterIDNo,JobIndustry,JobEntryDate,JobCompany,jobPosition,JobTitle,JobCompIndustry,JobCompProvince,JobCompCity,JobCompPhoneNo,JobCompZip,JobCompAddress,JobCompAreaNo,CurrProvince,CurrCity,CurrEntryDate,CurrAddressZip,CurrLivingCondition,PropertyAddType,CurrAddress,OwnEstate,PropertyLocation,PropertyOwner,PropertyLoan,OtherPropertyAddress,PhoneNo,CellNo,Email,RegiProvince,RegiCity,RegiPhoneNo,RegiZip,RegiAddress,IncomeYear,idCardCheckFlg,token) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) 
13:59:26.409 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20001} clearParameters. 
13:59:26.409 [main] DEBUG com.alibaba.druid.pool.PreparedStatementPool - {conn-10002, pstmt-20001} enter cache
13:59:26.409 [main] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT SCOPE_IDENTITY()
13:59:26.409 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20002} created
13:59:26.416 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20002} executed. 
SELECT SCOPE_IDENTITY()
13:59:26.416 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20002, rs-50001} query executed. 6.176854 millis. 
SELECT SCOPE_IDENTITY()
13:59:26.416 [main] DEBUG druid.sql.ResultSet - {conn-10002, stmt-20002, rs-50001} open
13:59:26.416 [main] DEBUG druid.sql.ResultSet - {conn-10002, stmt-20002, rs-50001} Header: []
13:59:26.416 [main] DEBUG druid.sql.ResultSet - {conn-10002, stmt-20002, rs-50001} Result: [null]
13:59:26.416 [main] DEBUG druid.sql.ResultSet - {conn-10002, stmt-20002, rs-50001} closed
13:59:26.416 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20002} closed
13:59:26.466 [main] DEBUG druid.sql.Connection - {conn-10002} commited
13:59:26.466 [main] DEBUG druid.sql.Connection - {conn-10002} setAutoCommit true
Disconnected from the target VM, address: '127.0.0.1:55124', transport: 'socket'
13:59:26.468 [main] DEBUG druid.sql.Connection - {conn-10002} pool-recycle
null    --打印主键是null

插数据没问题 就是在代码里拿不到 主键ID

恩? 这是改过之后的日志???

不是 跟了下代码 没进 org.nutz.dao.impl.jdbc.sqlserver2005.Sqlserver2005JdbcExpert.fetchPojoId(Entity<?>, MappingField) 这个方法 , 这个日志就是SELECT SCOPE_IDENTITY() 拿不到ID 的日志 。 实在是无解啊

我奇怪的是,现在没有出现connect close的日志了

既然没有close, 那就是同一个连接, 取不到值的话, 走jdbc也一样取不到呢

13:59:26.409 [main] DEBUG druid.sql.Statement - {conn-10002, pstmt-20001} clearParameters.
13:59:26.409 [main] DEBUG com.alibaba.druid.pool.PreparedStatementPool - {conn-10002, pstmt-20001} enter cache
13:59:26.409 [main] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT SCOPE_IDENTITY()
13:59:26.409 [main] DEBUG druid.sql.Statement - {conn-10002, stmt-20002} created

{conn-10002, pstmt-20001} 变成了 {conn-10002, stmt-20002} created 会是这个原因吗?

没改过啊 都是mvn 引入的jar包

我觉得用jdbc调用SELECT SCOPE_IDENTITY()也拿不到结果,你可以试试

添加回复
请先登陆
回到顶部