我在C#中制作的重型客户端正在通过Web方法与Java进行通信。 Java正在通过Hibernate调用数据库。 我创建了一个Web方法,允许从C#中的表中检索数据,以便在DB中插入,更新或删除。 我已经检查并调试了java中的web方法,并从C#中获取了良好的数据。 我一步一步检查了我的方法,一切似乎都很好,除了最后,这一行发回一个错误:
HibernateUtil.commitTransaction();
有我的方法:
public BatchSaveResponse setBaseFilter(BatchSaveRequest request) {
LOG.info("Executing operation setBaseFilter");
if (LOG.isDebugEnabled()) {
LOG.debug("Param: request "+ request);
}
MessageContext ctx = mc.getMessageContext();
HttpServletRequest req = (HttpServletRequest)ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
BatchSaveResponse _return = new BatchSaveResponse();
ObjectSet insertedObjects = request.getInsertedObjects();
ObjectKeySet deletedObjects = request.getDeletedObjectIds();
ObjectSet updatedObjects = request.getUpdatedObjects();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter insertedObjects = " + insertedObjects);
LOG.debug("SetBaseFilter deletedObjects = " + deletedObjects);
LOG.debug("SetBaseFilter updatedObjects = " + updatedObjects);
}
try {
String remoteUser = getUser(req);
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter remoteUser [" + remoteUser + "]");
}
HibernateUtil.beginTransaction(remoteUser);
// INSERTED OBJECTS
if (insertedObjects != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::start insert ...");
}
// Configure
ArrayOfCONFIGURE arrayOfCONFIGURE = insertedObjects.getCONFIGURE();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::insert , arrayOfCONFIGURE = " + arrayOfCONFIGURE);
}
if (arrayOfCONFIGURE != null) {
List<CONFIGURE> elements = arrayOfCONFIGURE.getCONFIGURE();
if (elements != null && elements.size() > 0) {
for (int i = 0; i < elements.size(); i++) {
CONFIGURE currentCONFIGURE = elements.get(i);
Configure configure = ConfigureDAOFactory.getDAO().getConfigureFromCONFIGURE(currentCONFIGURE);
if (configure != null) {
ConfigureDAOFactory.getDAO().createConfigure(configure);
}
}
}
}
Session session = HibernateUtil.currentSession();
session.flush();
session.clear();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::end insert.");
}
}
// UPDATED OBJECTS
if (updatedObjects != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::start update ...");
}
// Basefilter
ArrayOfBASEFILTER arrayOfBASEFILTER = updatedObjects.getBASEFILTER();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::update , arrayOfBASEFILTER = "+ arrayOfBASEFILTER);
}
if (arrayOfBASEFILTER != null) {
List<BASEFILTER> elements = arrayOfBASEFILTER.getBASEFILTER();
if (elements != null && elements.size() > 0) {
for (int i = 0; i < elements.size(); i++) {
BASEFILTER currentBASEFILTER = elements.get(i);
Basefilter basefilter = BasefilterDAOFactory.getDAO().getMinimalBasefilterFromBASEFILTER(currentBASEFILTER);
if (basefilter != null) {
BasefilterDAOFactory.getDAO().updateBasefilter(basefilter);
}
}
}
}
Session session = HibernateUtil.currentSession();
session.flush();
session.clear();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter end update!");
}
}
// DELETED OBJECTS
if (deletedObjects != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::start delete ...");
}
// Configure attribute
ArrayOfCONFIGUREKey arrayOfCONFIGUREKey = deletedObjects
.getCONFIGUREKeys();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::delete , arrayOfCONFIGUREKey = "
+ arrayOfCONFIGUREKey);
}
if (arrayOfCONFIGUREKey != null) {
List<CONFIGUREKey> elements = arrayOfCONFIGUREKey
.getCONFIGUREKey();
if (elements != null && elements.size() > 0) {
for (int i = 0; i < elements.size(); i++) {
CONFIGUREKey currentCONFIGUREKey = elements.get(i);
byte[] byteKey = currentCONFIGUREKey.getID();
ConfigureDAOFactory.getDAO()
.deleteConfigureForId(byteKey);
}
}
}
Session session = HibernateUtil.currentSession();
session.flush();
session.clear();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::end delete.");
}
}
HibernateUtil.commitTransaction();
} catch (DAOException e) {
LOG.error("SetBaseFilter::ERROR : " + e);
rollbackTransaction();
String message = getDAOMessage(e);
LOG.error("SetBaseFilter::ERROR message [" + message
+ "]");
throw new java.lang.UnsupportedOperationException(
"SetBaseFilter::ERROR = " + message);
} catch (Exception e) {
LOG.error("SetBaseFilter::ERROR = " + e);
rollbackTransaction();
throw new java.lang.UnsupportedOperationException(
"SetBaseFilter::ERROR "
+ e.getCause().getMessage());
} finally {
// No matter what happens, close the Session.
try {
HibernateUtil.closeSession();
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter::session closed!");
}
} catch (BaseConnectionServiceException e) {
LOG.error(e + " : cannot close session !");
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("SetBaseFilter end !");
}
return _return;
}
错误:
引起:java.lang.UnsupportedOperationException:SetBaseFilter :: ERROR =事务未成功启动。
SetBaseFilter :: ERROR:com。*** dao.DAOException:回滚事务时出错。
SetBaseFilter :: ERROR消息[事务未成功启动]。
我不明白为什么。在做任何事情之前我都会开始休眠,无论发生什么事我都会关闭它...谢谢!
编辑:
commitTransaction -
public void commitTransaction() throws BaseConnectionServiceException {
Connection s = (Connection) sessions.get();
if (s == null) {
throw new BaseConnectionServiceException("Connection doesn't exist.");
}
try {
s.commit();
s.setAutoCommit(true);
} catch (SQLException sqle) {
throw new BaseConnectionServiceException("Error when committing transaction.",
sqle);
}
return;
}
beginTransaction -
public void beginTransaction(String userName) throws BaseConnectionServiceException {
Connection s = (Connection) sessions.get();
if (s == null) {
createSession(userName);
s = (Connection) sessions.get();
}
try {
s.setAutoCommit(false);
} catch (SQLException sqle) {
throw new BaseConnectionServiceException("Error when begining transaction.",
sqle);
}
return;
}
答案 0 :(得分:1)
你在这里混合使用Hibernate和普通JDBC。
我建议您使用hibernate进行交易。
将您的代码重构为类似于此的内容:
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
//do some work
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}
答案 1 :(得分:0)
在beginTransaction
方法中使用@Transactional
。
如果方法只是一个查询,readonly = true
更可取:
@Transactional(readOnly = true)
public void beginTransaction(String userName) throws BaseConnectionServiceException {
答案 2 :(得分:0)
值得一看的还有createSession()的代码。但乍一看,您似乎使用Hibernate访问数据库,但您使用低级JDBC来管理事务。这不行。您必须在Session.beginTransaction上启动事务才能实现目标:https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html#beginTransaction()
总的来说,我建议避免自己的会话管理并使用注释驱动的事务。这将导致更安全,可扩展,您无需重新发明轮子。
答案 3 :(得分:0)
问题来自于在我的setBaseFilter()之前运行的另一个方法。实际上由于一个名为getBasefilter()的方法填充了一个表,其中有一个HibernateUtil.beginTransaction()但没有HibernateUtil.commitTransaction()。所以事务是打开的,当我调用setBaseFilter来保存选项卡中的修改时,我再次启动一个Hibernate beginTransaction而不关闭前一个。
感谢大家的帮助! 继续前进:)