我的Web应用程序使用Spring(4.2.9),Spring Data(3.2.5),JPA,Hibernate(4.3.8)和MS SQL Server(2014)。以下是控制器方法中的流程:
ObjectA = service.getObjectAById(some_id);
o.setUpdate(new Date()); //for temporary need.
service.createObjectBAndSaveIt(new ObjectB());
以下是服务层中的两个相关方法:
@Override
@Transactional(readOnly = true)
public ObjectA getObjectAById(Long id) {
//load ObjectA from database
}
@Override
public void createObjectBAndSaveIt(Long id) {
//create and save ObjectB
}
请注意,ObjectA和ObjectB没有直接或间接的关系。 ObjectA的版本字段如下:
@Version
private Long version = 1L;
注意到,当执行上述Web流时,ObjectA的版本号增加1,即使它未在service.createObjectBAndSaveIt
中引用或使用。根据我所知,Hibernate在执行service.createObjectBAndSaveIt
时会进行脏检查并保存OjbectA。我不想要这种默认行为。如何使系统仅保留显示在transactinal方法内的对象(如ObjectB)?任何指针或示例都会非常感激。
答案 0 :(得分:1)
Spring使用默认情况下所需的事务传播。尝试使用@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
使用@Transactional(readOnly = true)
显式注释方法,以处理要保留的数据(在本例中为对象B),并将library(NLP)
library(openNLP)
library(dplyr)
s <- "The room temperature is 37 to 39 C. The Air flow is near 80 cfm"
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- annotate(s, list(sent_token_annotator, word_token_annotator))
pos_tag_annotator <- Maxent_POS_Tag_Annotator()
a3 <- annotate(s, pos_tag_annotator, a2)
#distribution of POS tags for word tokens
a3w <- subset(a3, type == "word")
#select consecutive NN & CD POS
a3w_temp <- a3w[sapply(a3w$features, function(x) x$POS == "NN" | x$POS == "CD")]
a3w_temp_df <- as.data.frame(a3w_temp)
#add lead 'features' to dataframe and select rows having (NN, CD) or (NN, CD, CD) sequence
a3w_temp_df$ahead_features = lead(a3w_temp_df$features,1)
a3w_temp_df$features_comb <- paste(a3w_temp_df$features,a3w_temp_df$ahead_features)
l <- row.names(subset(a3w_temp_df, features_comb == "list(POS = \"NN\") list(POS = \"CD\")" |
features_comb == "list(POS = \"CD\") list(POS = \"CD\")"))
l_final <- sort(unique(c(as.numeric(l), as.numeric(l) +1)))
a3w_df <- a3w_temp_df[l_final,]
#also include POS which is immediately after CD
idx <- a3w_df[a3w_df$features=="list(POS = \"CD\")","id"]+1
idx <- sort(c(idx,a3w_df$id))
op = paste(strsplit(s, split = " ")[[1]][idx -1], collapse = " ")
op
用于非写入数据(在本例中为对象A)
Spring文档指出:
默认的@Transactional设置如下:
传播设置为PROPAGATION_REQUIRED。隔离级别是 ISOLATION_DEFAULT。交易是可读/写的。