Skip to content

Instantly share code, notes, and snippets.

@SlyDen
Created January 25, 2025 09:08
Show Gist options
  • Save SlyDen/0e5340e4dd8e6ccff95cf44dfa546d2e to your computer and use it in GitHub Desktop.
Save SlyDen/0e5340e4dd8e6ccff95cf44dfa546d2e to your computer and use it in GitHub Desktop.
JNDI stub
package com.example.config;
import com.example.interceptor.CustomHibernateInterceptor;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
/*
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jndi</artifactId>
<version>5.3.x</version>
<scope>test</scope>
</dependency>
In your test class, the JNDI DataSource will now be available. You don’t need to change anything in the test logic itself since Spring will handle the injection.
Key Notes:
1. Apache Naming Factory: The org.apache.naming.java.javaURLContextFactory is used to set up a JNDI context for testing.
2. H2 Database: The JdbcDataSource from H2 is bound to the JNDI name java:/comp/env/jdbc/MyDataSource.
3. Binding Context: The InitialContext creates the JNDI subcontext and binds the data source to it.
4. JNDI Lookup: In your application, ensure that any DataSource lookup from JNDI (e.g., java:/comp/env/jdbc/MyDataSource) matches the name bound in the test configuration.
*/
@Configuration
public class TestHibernateConfig {
@Bean
public DataSource dataSource() {
try {
// Set up a JNDI context
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext context = new InitialContext();
context.createSubcontext("java:/comp/env");
// Create H2 DataSource and bind it to JNDI
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
dataSource.setUser("sa");
dataSource.setPassword("");
context.bind("java:/comp/env/jdbc/MyDataSource", dataSource);
return dataSource;
} catch (NamingException e) {
throw new RuntimeException("Failed to bind DataSource to JNDI", e);
}
}
@Bean
public LocalSessionFactoryBean sessionFactory(CustomHibernateInterceptor customHibernateInterceptor, DataSource dataSource) {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setPackagesToScan("com.example.entity");
sessionFactory.setDataSource(dataSource);
sessionFactory.getHibernateProperties().put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
sessionFactory.getHibernateProperties().put("hibernate.hbm2ddl.auto", "create-drop");
sessionFactory.getHibernateProperties().put("hibernate.show_sql", "true");
sessionFactory.getHibernateProperties().put("hibernate.ejb.interceptor", customHibernateInterceptor);
return sessionFactory;
}
@Bean
public JpaTransactionManager transactionManager(SessionFactory sessionFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(sessionFactory);
return transactionManager;
}
@Bean
public CustomHibernateInterceptor customHibernateInterceptor() {
return new CustomHibernateInterceptor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment