我在从JSP文件访问集合时遇到问题。应用程序基于MVC Spring框架。我把OpenSessionInViewFilter过滤器放在我的web.xml. 当我想使用提到的文件访问 URL 时,它会抛出我
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.cloud.hibernate.Product.availabilities, no session or session was closed
我在我的应用程序中使用域驱动设计。当我将类中的 fetch 类型Product从更改FetchType.LAZY为 时FetchType.EAGER,它起作用了。
堆栈跟踪
SEVERE: Servlet.service() for servlet [spring] in context with path [] threw exception [org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.cloud.hibernate.Product.availabilities, no session or session was closed] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.cloud.hibernate.Product.availabilities, no …Run Code Online (Sandbox Code Playgroud) Spring MVC应用程序需要执行一个需要几分钟的密集计算工作.客户端希望以异步方式运行此作业.但是在我对方法启用@Async注释后(参见代码1)并获得错误(参见代码2).我的web.xml和web-appliaiton-context.xml也在下面给出.我试图找到解决方法来解决这个问题,但失败了.请帮忙.
代码1:
@Service
public class AsyncServiceImpl implements AsyncServiceInt{
protected int pertArrSize = 1000;
@Autowired
protected TblvDao tblvDao1 = null;
@Override
@Async
public void startSlowProcess(Integer scenarioId) throws SecurityException, IllegalArgumentException, IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
batchUpdateSummaryPertSim(scenarioId);
}
public void batchUpdateSummaryPertSim(Integer scenarioId) throws SecurityException, IllegalArgumentException, IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException
{
double[] summaryArry = new double[pertArrSize];
summaryArry = summaryPertSim_env(scenarioId, summaryArry);
}
@Transactional
private double[] summaryPertSim_env(Integer scenarioId,
double[] summaryArry) throws IOException, ClassNotFoundException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException {
ScenarioTblv scenario = tblvDao1.getScenarioTblv(scenarioId);
TblvResultSaved savedResult = …Run Code Online (Sandbox Code Playgroud) 我想在 C++ 类中进行“懒惰构造”,一个简单的方法是这样的:
#include "b.hpp" // class B
class A {
public:
// invoke B() in b.hpp, this constructor do not actually do init
A(int v_in) : v(v_in) {}
void foo() {
if(flag == false) {
b = create_B_obj();
flag = true;
}
}
private:
B create_B_obj() {
// some expensive ops
//...
}
private:
bool flag = false;
B b;
int v;
};
Run Code Online (Sandbox Code Playgroud)
但是存在的一个问题是 B 可能不包含默认构造函数 ( B()),那么在这种情况下我该如何做“懒惰构造”呢?
顺便说一句:我项目中的 B 类就像一个套接字,需要进行连接或类似绑定的调用,所以我想把这些昂贵的操作懒惰。
c++ sockets constructor default-constructor lazy-initialization
LazyInitializationException当我尝试访问分离实体的惰性 @ManyToOne 引用的 ID 时,我正面临着这种情况。我不想完全获取引用,而只需要 ID(它应该存在于原始对象中,以便以惰性/延迟方式获取引用)。
EntityA ea = dao.find(1) // find is @Transactional, but transaction is closed after method exits
ea.getLazyReference().getId() // here is get exception. lazyReference is a ManyToOne relation and so the foreight key is stored in EntityA side.
Run Code Online (Sandbox Code Playgroud)
换句话说,如何在不实际获取整个 LazyReference 的情况下访问 LazyReference 的 ID(实际上存在于 EntityA 的初始选择中)?
以下内容与其说是一个问题,不如说是一个评估请求。
因此,您很可能熟悉以下惰性 getter 模式。
private Object obj;
public Object getObject() {
if(obj==null) {
obj = new Object();
}
return obj;
}
Run Code Online (Sandbox Code Playgroud)
那个代码
所以最近一个同事和我想出了以下界面(简化):
public interface LazyGetterSupport {
default Object get(Supplier<Object> impl) {
String key = impl.getClass().getName();
Object retVal;
if ((retVal = getInstanceCache().get(key)) == null) {
retVal = impl.get();
getInstanceCache().put(key, retVal);
}
return retVal;
}
Map<String, Object> getInstanceCache();
}
Run Code Online (Sandbox Code Playgroud)
旁注:不使用 HashMap#computeIfAbsent bc of Bug-8071667
然后该接口由您要使用惰性获取器的类实现。您需要提供getInstanceCache()如下实现:
private Map<String, Object> instanceCache;
@Override
public Map<String, Object> getInstanceCache() …Run Code Online (Sandbox Code Playgroud) java getter lazy-loading lazy-evaluation lazy-initialization
有没有办法将Name属性作为参数传递给Lazy BOM Initialization?
public class Item
{
private Lazy<BOM> _BOM = new Lazy<BOM>(); // How to pass the Name as parameter ???
public Item(string name)
{
this.Name = name;
}
public string Name { get; private set; }
public BOM BOM { get { return _BOM.Value; } }
}
public class BOM
{
public BOM (string name)
{
}
}
Run Code Online (Sandbox Code Playgroud) spring-boot 中有两个 IOC 容器:BeanFactory和ApplicationContext。
根据我的理解,ApplicationContext支持bean的急切初始化,在哪里BeanFactory延迟初始化。
问题陈述:在我的 Spring boot 应用程序中,我想使用 bean 的延迟初始化来使应用程序启动更快。任何人都可以建议实现相同目标的解决方案吗?
java performance lazy-initialization applicationcontext spring-boot
我是 Rust 新手。我正在尝试在库中创建一个静态变量,DATA以便Vec<u8>在编译库后对其进行初始化。然后我将该库包含在主代码中,希望能够DATA直接使用而无需再次调用init_data()。这是我尝试过的:
my_lib.rs:
use lazy_static::lazy_static;
pub fn init_data() -> Vec<u8> {
// some expensive calculations
}
lazy_static! {
pub static ref DATA: Vec<u8> = init_data(); // supposed to call init_data() only once during compilation
}
Run Code Online (Sandbox Code Playgroud)
主要.rs:
use my_lib::DATA;
call1(&DATA); // use DATA here without calling init_data()
call2(&DATA);
Run Code Online (Sandbox Code Playgroud)
但事实证明,init_data()仍然是在调用main.rs。这段代码有什么问题?
更新:正如 Ivan C 指出的那样,lazy_static它不在编译时运行。那么,“预加载”数据的正确选择是什么?
我不确定如何初始化一个C结构或iOS5之前的类型的类属性.如果我正在处理一个类,这就是我要做的事情,但是我不知道我可以检查这是否是第一次访问该结构,因为它们在创建时未定义:
@interface GraphView : UIView
@property (nonatomic) CGPoint origin;
@end
@implementation GraphView
@synthesize origin = _origin;
- (CGPoint)origin
{
if (WHAT?) {
_origin = CGPointMake(self.bounds.origin.x + self.bounds.size.width/2,
self.bounds.origin.y + self.bounds.size.height/2);
}
return _origin;
}
@end
Run Code Online (Sandbox Code Playgroud)
我意识到延迟初始化的主要好处是内存分配,但如果我这样做是为所有的类的属性,似乎清晰的使用相同的风格对我的所有属性设置的初始值.
我可以使用其他一些实例变量或属性来跟踪是否已访问self.origin,但这似乎......不顺利.在设置它之前,我可以注意永远不要访问self.origin,这似乎有点因为结构在创建时未定义.
有"正确"的方式吗?
我找不到任何关于如何将Autofac与Lazy和生命周期范围一起使用的文档.得到一个错误
"从请求实例的范围中看不到带有匹配'事务'的标记的范围......"
在我的Controller构造函数中:
public HomeController(Lazy<ISalesAgentRepository> salesAgentRepository, Lazy<ICheckpointValueRepository> checkpointValueRepository)
{
_salesAgentRepository = new Lazy<ISalesAgentRepository>(() => DependencyResolver.Current.GetService<ISalesAgentRepository>());
_checkpointValueRepository = new Lazy<ICheckpointValueRepository>(() => DependencyResolver.Current.GetService<ICheckpointValueRepository>());
}
Run Code Online (Sandbox Code Playgroud)
在我的行动中:
using (var transactionScope = AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope("transaction"))
{
using (var repositoryScope = transactionScope.BeginLifetimeScope())
{
// ....
}
}
Run Code Online (Sandbox Code Playgroud)
生命范围与Lazy不兼容还是我完全错了?
c# asp.net-mvc dependency-injection autofac lazy-initialization
java ×3
c# ×2
hibernate ×2
lazy-loading ×2
spring ×2
asp.net-mvc ×1
asynchronous ×1
autofac ×1
c++ ×1
constructor ×1
getter ×1
ios ×1
jsp ×1
lazy-static ×1
many-to-one ×1
objective-c ×1
performance ×1
rust ×1
sockets ×1
spring-boot ×1
spring-mvc ×1
struct ×1