mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-08-06 12:11:13 +00:00
WIP Multiple DataSources working
This commit is contained in:
@@ -1,101 +1,72 @@
|
|||||||
package tech.ailef.dbadmin;
|
package tech.ailef.dbadmin;
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.hibernate.jpa.HibernatePersistenceProvider;
|
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
|
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import tech.repo.Action;
|
||||||
|
|
||||||
@ConditionalOnProperty(name = "dbadmin.enabled", matchIfMissing = true)
|
@ConditionalOnProperty(name = "dbadmin.enabled", matchIfMissing = true)
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
@EnableConfigurationProperties(DbAdminProperties.class)
|
@EnableConfigurationProperties(DbAdminProperties.class)
|
||||||
//@AutoConfiguration
|
@Configuration
|
||||||
@EnableJpaRepositories(
|
@EnableJpaRepositories(
|
||||||
entityManagerFactoryRef = "internalEntityManagerFactory",
|
entityManagerFactoryRef = "internalEntityManagerFactory",
|
||||||
transactionManagerRef = "internalTransactionManager",
|
transactionManagerRef = "internalTransactionManager",
|
||||||
basePackages = { "tech.repo" }
|
basePackages = { "tech.repo" }
|
||||||
)
|
)
|
||||||
|
@EnableTransactionManagement
|
||||||
public class DbAdminAutoConfiguration {
|
public class DbAdminAutoConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
Environment env;
|
Environment env;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource internalDataSource() {
|
public DataSource internalDataSource() {
|
||||||
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
|
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
|
||||||
dataSourceBuilder.driverClassName("org.h2.Driver");
|
dataSourceBuilder.driverClassName("org.h2.Driver");
|
||||||
dataSourceBuilder.url("jdbc:h2:file:./dbadmin_internal");
|
dataSourceBuilder.url("jdbc:h2:file:./dbadmin_internal");
|
||||||
dataSourceBuilder.username("sa");
|
dataSourceBuilder.username("sa");
|
||||||
dataSourceBuilder.password("password");
|
dataSourceBuilder.password("password");
|
||||||
return dataSourceBuilder.build();
|
return dataSourceBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory() {
|
public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory() {
|
||||||
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
|
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
|
||||||
factoryBean.setDataSource(internalDataSource());
|
factoryBean.setDataSource(internalDataSource());
|
||||||
factoryBean.setPersistenceUnitName("internal");
|
factoryBean.setPersistenceUnitName("internal");
|
||||||
factoryBean.setPersistenceProvider(new HibernatePersistenceProvider());
|
factoryBean.setPackagesToScan("tech.repo"); // , "tech.ailef.dbadmin.repository");
|
||||||
// factoryBean.setManagedTypes(new PersistenceManagedTypes() {
|
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||||
//
|
Properties properties = new Properties();
|
||||||
// @Override
|
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
|
||||||
// public URL getPersistenceUnitRootUrl() {
|
properties.setProperty("hibernate.hbm2ddl.auto", "update");
|
||||||
// return null;
|
factoryBean.setJpaProperties(properties);
|
||||||
// }
|
factoryBean.afterPropertiesSet();
|
||||||
//
|
return factoryBean;
|
||||||
// @Override
|
}
|
||||||
// public List<String> getManagedPackages() {
|
|
||||||
// return List.of("tech.ailef.dbadmin.model", "tech.ailef.dbadmin.repository");
|
@Bean
|
||||||
// }
|
public PlatformTransactionManager internalTransactionManager() {
|
||||||
//
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
// @Override
|
transactionManager.setEntityManagerFactory(internalEntityManagerFactory().getObject());
|
||||||
// public List<String> getManagedClassNames() {
|
return transactionManager;
|
||||||
// return List.of("tech.ailef.dbadmin.model.Action");
|
}
|
||||||
// }
|
|
||||||
// });
|
|
||||||
factoryBean.setPackagesToScan("tech.repo"); //, "tech.ailef.dbadmin.repository");
|
|
||||||
// List<ManagedType> ts = new ArrayList<>();
|
|
||||||
// factoryBean.setManagedTypes(ts);
|
|
||||||
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
|
||||||
Properties properties = new Properties();
|
|
||||||
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
|
|
||||||
properties.setProperty("hibernate.hbm2ddl.auto", "update");
|
|
||||||
factoryBean.setJpaProperties(properties);
|
|
||||||
factoryBean.afterPropertiesSet();
|
|
||||||
return factoryBean;
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Bean
|
|
||||||
// public EntityManager getInternalEntityManager() {
|
|
||||||
//// if (internalEntityManager == null) {
|
|
||||||
// LocalContainerEntityManagerFactoryBean emf = internalEntityManager();
|
|
||||||
// EntityManagerFactory factory = emf.getNativeEntityManagerFactory();
|
|
||||||
// return factory.createEntityManager();
|
|
||||||
//// }
|
|
||||||
//// return internalEntityManager;
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PlatformTransactionManager internalTransactionManager() {
|
|
||||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(internalEntityManagerFactory().getObject());
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -13,6 +13,7 @@ import org.springframework.dao.DataIntegrityViolationException;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.jdbc.UncategorizedSQLException;
|
import org.springframework.jdbc.UncategorizedSQLException;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
@@ -25,9 +26,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
import org.springframework.web.server.ResponseStatusException;
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import jakarta.persistence.EntityManagerFactory;
|
import jakarta.persistence.EntityManagerFactory;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
import jakarta.persistence.PersistenceUnit;
|
import jakarta.persistence.PersistenceUnit;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
@@ -59,18 +60,9 @@ public class DefaultDbAdminController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DbAdmin dbAdmin;
|
private DbAdmin dbAdmin;
|
||||||
|
|
||||||
@PersistenceUnit(unitName = "internal")
|
|
||||||
private EntityManagerFactory entityManagerFactory;
|
|
||||||
|
|
||||||
// private EntityManager entityManager;
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ActionRepository repo;
|
private ActionRepository repo;
|
||||||
|
|
||||||
// @PostConstruct
|
|
||||||
// public void initialize() {
|
|
||||||
// this.entityManager = entityManagerFactory.createEntityManager();
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Home page with list of schemas
|
* Home page with list of schemas
|
||||||
* @param model
|
* @param model
|
||||||
@@ -78,11 +70,13 @@ public class DefaultDbAdminController {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@GetMapping
|
@GetMapping
|
||||||
// @Transactional("internalTransactionManager")
|
@Transactional("internalTransactionManager")
|
||||||
public String index(Model model, @RequestParam(required = false) String query) {
|
public String index(Model model, @RequestParam(required = false) String query) {
|
||||||
Action a = new Action();
|
Action a = new Action();
|
||||||
a.setDescription("ciao");
|
a.setDescription("ciao");
|
||||||
// a.setId(1);
|
// a.setId(1);
|
||||||
|
// entityManagerFactory.createEntityManager().persist(a);
|
||||||
|
// entityManager.persist(a);
|
||||||
Action save = repo.save(a);
|
Action save = repo.save(a);
|
||||||
System.out.println(save);
|
System.out.println(save);
|
||||||
|
|
||||||
|
@@ -8,7 +8,6 @@ import org.springframework.stereotype.Repository;
|
|||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
//@Component
|
|
||||||
public interface ActionRepository extends JpaRepository<Action, Integer> {
|
public interface ActionRepository extends JpaRepository<Action, Integer> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user