Skip to content

Cannot Lazily Instantiate Hibernate LocalSessionFactoryBean using LazyInitTargetSource [SPR-1695] #6392

@spring-projects-issues

Description

@spring-projects-issues

Stephen Duncan Jr opened SPR-1695 and commented

If I specify a ProxyFactoryBean with a targetSource of type LazyInitTargetSource which targets a LocalSessionFactoryBean, the underlying Hibernate SessionFactory is created anyway.

The problem is that to create the proxy, the LazyInitTargetSource must determine the type of the object is must proxy. So it calls getObjectType on the bean. Because the bean is a FactoryBean, it must instantiate the FactoryBean to call getObjectType on it. This, however, causes the SessionFactory to be created, because that is done in the afterPropertiesSet method.

One solution would be to modify LocalSessionFactoryBean to delay instantiation of the SessionFactory until the getObject method is called.

Another might be to first attempt to call getObjectType on the factory before setting any properties. That way if the value is known beforehand (not dynamically), the rest of the methods wouldn't be called.

The last solution that occurs to me is to provide another way to specify the object type. Such as having it be a property you could set on the LazyInitTargetSource. This would prevent instantiation of the Factory even, and therefore would seem to honor the intentions of not instantiating what you're proxying better.


Affects: 1.2.6

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)in: dataIssues in data modules (jdbc, orm, oxm, tx)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions