spring如何知道要使用哪个连接池? [英] how does spring know which connection pool to use?
问题描述
spring如何知道要使用哪个连接池?
How does spring know which connection pool to use?
众所周知,您要给Spring框架一个持久性单元名称,并使用@PersistenceContext和配置的Persistence.xml注释实体管理器.Spring会为您做所有事情.
As is known ,you tell the spring framework a persistence-unit name,and annotate the entity manager with @PersistenceContext,and with Persistence.xml configured.Spring does every thing for you.
对于entityManager字段上方的弹簧注释"@PersitenceContext",我感到非常困惑. 我的persistence.xml如下:
I am very confused about the spring annotation "@PersitenceContext" above the entityManager field. My persistence.xml is as below :
<persistence-unit name="hibernate.recommendation_report.jpa">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@192.168.113.226:11521:BOSS" />
<property name="javax.persistence.jdbc.user" value="xxxx" />
<property name="javax.persistence.jdbc.password" value="xxxx" />
</properties>
</persistence-unit>
tomcat服务器启动后的短时间内,我的tomcat服务器和web应用程序运行良好.但是几个小时后,该服务器报告一个sqlexception连接已关闭".
My tomcat server and my webapp work well when and after for a short peoriod of time after the starting up of tomcat server.But hours later,the server reports a sqlexception "Connection already closed".
这是滥用数据库连接池的问题吗? spring框架如何选择c3p0或DBCP?我将如何指定连接池?还是tomcat使用默认的DBCP作为连接池?
Is this the problem of misusing db connection pool? How do the spring framework choose a c3p0 or DBCP ? How would I specify the connection pool? Or is the tomcat uses the default DBCP as the connection pool?
推荐答案
U可以使您的tomcat服务器或其他应用服务器提供JNDI数据源.这样,服务器容器的自包含连接池就可以很好地照顾数据库连接/会话. 对于您的Tomcat,您可以在$ TOMCAT_HOME/conf/context.xml或server.xml中指定JNDI数据源:
U can make your tomcat server or other app server provide the JNDI datasource. So that, the your server container's self-contained connection pool can take good care of your the database connection/session. Tomcat in your case, you specify the JNDI datasource in the $TOMCAT_HOME/conf/context.xml or server.xml:
<Resource name="jdbc/sample" auth="Container"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
username=...
password=...
url=...
driverClassName=...
/>
type属性告诉tomcat使用哪个连接池. 默认情况下,tomcat6使用类型为"java.sql.DataSource" 的DBCP. 确保在persistence.xml中使用JNDI引用:
the type attribute tells the tomcat which connection pool to use. By default tomcat6 uses DBCP with type of "java.sql.DataSource". Make sure to use the JNDI reference in your persistence.xml:
<persistence version="2.1" ....>
<persistence-unit name="hibernate.recommendation_report.jpa">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java:comp/env/jdbc/sample</non-jta-data-source>
</persistence-unit>
</persistence>
或者使用spring xml来配置数据源bean以注入到您的entityManagerFactory中.请
Or use spring xml to config the datasource bean to inject into the your entityManagerFactory.Please refer to here.
<jee:jndi-lookup id="dataSource" jndi-name="java:sample"/>
请参阅其他JNDI资源可用于DBCP的属性. 注意:persistence.xml数据源中的"java:comp/env/"前缀非常重要.没有它,Spring不会寻找应用服务器提供的池来获取数据源,而只是使用该属性来构造一个简单的数据源. 注意:tomcat8本身提供了一个更好的池.如果升级到tomcat8.
See other JNDI resource attributes available for DBCP. Note: the "java:comp/env/" prefix in persistence.xml data-source is very import.Without it, Spring will not look for the pool provided by your application server to fetch datasource but just use the attribute to construct a simple datasource. Note: tomcat8 itself provides a even better pool.If you upgrade to tomcat8.
这篇关于spring如何知道要使用哪个连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!