Java:无法在Java 1.4 API中使用Value False设置自动提交模式?
-
19-09-2019 - |
题
SQL Server 200 Java 1.4 JBOSS 3
嗨,我会得到异常消息“您无法在托管交易期间设置自动加入”
代码在下面
try {
try {
connection = getConnection();
} catch (Exception e) {
throw new ConnectionException(e.getMessage());
}
for(int i=0;i<recordIds.size();i++)
{
String currentRecordId=(String)recordIds.get(i);
try
{
//exception on this line connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(getSQL("PurgeRecordInDumpData"));
preparedStatement.setLong(1,Long.parseLong(currentRecordId));
int numberOfUpdates=preparedStatement.executeUpdate();
if(numberOfUpdates!=1)
{
throw new Exception("Record with record id "+currentRecordId +"could not be purged.");
}
preparedStatement.close();
connection.commit();
listOfPurgedRecords.add(currentRecordId);
}
catch(Exception e)
{
connection.rollback();
}
}
return listOfPurgedRecords;
}
该例外的原因是什么?这是什么意思?
解决方案
错误是明确的,您无法在托管事务中设置自动企业。您甚至不需要将其设置为false,因为默认值是启用自动加入的。
我不确定您是否正在使用J2EE和EJB的,如果您想启用自动加入,则可以将设置更改为BEAN托管交易(BMT),这将使您可以修改此设置。
但是,您在代码中使用它的方式无需将其设置为false,所有内容都是在交易中完成的,并且可以通过提交或回滚控制它们。
不隶属于 StackOverflow