java: 无法在 java 1.4 api 中设置值为 false 的自动提交模式?
sql服务器200 爪哇1.4 jboss 3
HI 收到异常消息 “您无法在托管事务期间设置自动提交”
代码如下,
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;
}
此异常的原因是什么?它是什么意思?
sql server 200
java 1.4
jboss 3
HI am getting exception message
"You cannot set autocommit during a managed transaction"
code is below
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;
}
what is cause of this exception and what does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误很明显,当您处于托管事务中时,无法设置自动提交。您甚至不需要将其设置为 false,因为这是默认值,您可以使用它来启用它自动提交。
我不确定您是否使用 J2EE 和 EJB,如果您使用 J2EE 和 EJB,并且您想要启用自动提交,您可以将您的设置更改为 bean 管理事务 (BMT),这将允许您修改此设置。
但是,您在代码中使用它的方式不需要将其设置为 false,一切都在事务中完成,并且您可以通过提交或回滚来控制它们。
The error is clear, you cannot set autocommit while you are in a managed transaction. You should not even need to set this to false as that is the default, you use to enable it autocommit.
I am not sure if you are using J2EE and EJB's, if you are and you want to ENABLE autocommit, you can change your setting to bean managed transaction (BMT) and this would allow you to modify this setting.
However, the way you are using it in your code you don't need to set it to false, everything is done in transactions and you control them with commit or rollback.