Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hibernate 4 class loading issue on JBoss 7 when hibernate & spring classes loaded by different classloaders [SPR-9846] #14479

Closed
spring-projects-issues opened this issue Oct 1, 2012 · 7 comments
Assignees
Labels
type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 1, 2012

Maksym Gryevtsov opened SPR-9846 and commented

If you try to deploy the application on JBoss 7 using the built-in hibernate, you got this stack trace:
15:32:46,919 ERROR [org.hibernate.internal.SessionFactoryImpl] (MSC service thread 1-1) HHH000302: Unable to construct current session context [org.springframework.orm.hibernate4.SpringSessionContext]: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.springframework.orm.hibernate4.SpringSessionContext]
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at org.hibernate.internal.SessionFactoryImpl.buildCurrentSessionContext(SessionFactoryImpl.java:1491) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:502) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1746) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1784) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242) [spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372) [spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357) [spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]

After digging the code it looks like that the spring initialized hibernate in a way that "hibernate.classLoader.application" property is not used. As result hibernate try to use local classloader for loading class defined in "hibernate.current_session_context_class"


Affects: 3.1.2

Issue Links:

  • #15993 java.security.AccessControlException: Access denied (java.lang.RuntimePermission createClassLoader)
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 1, 2012

Maksym Gryevtsov commented

Please include a patch into 3.1.3; without it spring+hibernate applications are not deployable on JBoss 7

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 2, 2012

Maksym Gryevtsov commented

You might follow https://community.jboss.org/message/762735

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 4, 2012

Juergen Hoeller commented

LocalSessionFactoryBean does actually set that Hibernate property through using LocalSessionFactoryBuilder's (DataSource dataSource, ResourceLoader resourceLoader) constructor...

How are you setting up your LocalSessionFactoryBean? As a standard bean definition i a Spring ApplicationContext? Could you please double-check whether setResourceLoader is being called on your LocalSessionFactoryBean?

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 4, 2012

Maksym Gryevtsov commented

Juergen,

You are correct regarding Spring setting that property, except that Hibernate 4.1.6 & up do not use it anymore. It is absolute. I already fixed the error. Please apply my patch to the codebase (you can optimize it since that Hibernate property is not in use anymore). It's tested with JBoss 7.1.3.Final

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.service.BootstrapServiceRegistry;
import org.hibernate.service.BootstrapServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistryBuilder;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;

public class LocalSessionFactoryBean
extends org.springframework.orm.hibernate4.LocalSessionFactoryBean {

@Override
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
ClassLoader appClassLoader = (ClassLoader) sfb.getProperties().get(AvailableSettings.APP_CLASSLOADER);
BootstrapServiceRegistryBuilder bootstrapServiceRegistryBuilder = new BootstrapServiceRegistryBuilder()
.withApplicationClassLoader(appClassLoader);
BootstrapServiceRegistry bootstrapServiceRegistry = bootstrapServiceRegistryBuilder.build();
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder(bootstrapServiceRegistry).applySettings(sfb.getProperties());
return sfb.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
}
}

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 8, 2012

Juergen Hoeller commented

It seems there is actually no proper way to initialize Hibernate such that the property "hibernate.classLoader.application" is being used, other than manual invocation of ClassLoaderServiceImpl's fromConfigSettings method. I'd consider it a Hibernate bug to offer a configuration property that is not being checked at runtime by default...

Anyway, your ServiceRegistry building code does seem to work, so we might use some variant of that in LocalSessionFactoryBuilder. Alternatively, we could also set the thread context ClassLoader: We're doing that in our LocalSessionFactoryBean for Hibernate 3, and it seems that Hibernate 4 picks up the thread context ClassLoader by default as well.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 8, 2012

Maksym Gryevtsov commented

I'm fine with any solution as long as it works with local spring jar inside EAR or WAR \lib, and hibernate jars loaded from JBoss 7 org.hibernate module (parent class loader).
Thanks

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 10, 2012

Juergen Hoeller commented

I went the thread context ClassLoader solution since that one is not dependent on Hibernate service impl APIs (which come with no guarantee of stability).

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug
Projects
None yet
Development

No branches or pull requests

2 participants