大家好我使用Hibernate + Struts2 + Tomcat6 + Mysql作为我的J2EE框架。 我一直在使用hibernate的内置连接池机制,但事实certificate,在8小时之后,mysqlclosures它的连接时,会出现问题。 无论如何,我search了一下,发现我应该通过JNDI数据源获得我的连接,但我无法达成一个好的和完整的教程。 我应该采取什么措施来做到这一点? 请给我足够的细节,我有点新。 这里是我的hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/hposg?characterEncoding=UTF-8 </property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.max_fetch_depth">3</property> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.idle_test_period">300</property> <property name="hibernate.c3p0.timeout">1800</property> […]
import org.apache.catalina.Context; import org.apache.catalina.deploy.ContextResource; import org.apache.catalina.startup.Tomcat; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @EnableAutoConfiguration @ComponentScan @ImportResource("classpath:applicationContext.xml") public class Application { public static void main(String[] args) throws Exception { new SpringApplicationBuilder() .showBanner(false) .sources(Application.class) .run(args); } @Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { […]
在Spring的javadoc文章中有关于DriverManagerDataSource类的说法,这个类很简单,推荐使用 使用容器提供的JNDI数据源。 这样的DataSource可以通过JndiObjectFactoryBean在Spring ApplicationContext中作为DataSource bean公开 问题是:如何做到这一点? 例如,如果我希望有DataSource bean来访问我的custo mysql数据库,那么我需要什么? 在上下文configuration等写什么?