2013-04-11 21 views
2

由於某些原因,我需要微調jdbc connecton,並且我發現,context.xml是我可以做到的。 (http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#PostgreSQL如何爲Hibernate設置Tomcat 7 Postgresql數據源?

所以,我創建的context.xml:

<Context> 
<Resource 
    name="jdbc/connectorDs" 
    auth="Container" 
    type="javax.sql.DataSource" 
    driverClassName="org.postgresql.Driver" 
    url="jdbc:postgresql://localhost/somedb" 
    username="pguser" 
    password="pgpw" 
    maxActive="20" 
    maxIdle="10" 
    maxWait="-1" /> 
</Context> 

添加一些配置到web.xml中:

<resource-ref> 
    <description>postgreSQL Datasource example</description> 
    <res-ref-name>jdbc/connectorDs</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

並修改hibernate.cfg.xml中:

<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
<property name="connection.driver_class">org.postgresql.Driver</property> 
<property name="hibernate.connection.datasource">java:jdbc/connectorDs</property> 

<!-- THE ORIGINAL CONFIG --> 
<!-- <property name="connection.url">jdbc:postgresql://localhost/somedb</property> --> 
<!-- <property name="connection.username">pguser</property> --> 
<!-- <property name="connection.password">pgpw</property> --> 

但是這種konfig不起作用:SEVERE: Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not find datasource。我想數據源不存在或數據源連接字符串java:jdbc/connectorDs是不正確的。有人有任何經驗嗎?如何設置我的連接屬性,或者如何設置連接上的其他屬性?

在此先感謝。

回答

2

更換

<property name="hibernate.connection.datasource">java:jdbc/connectorDs</property> 

嘗試

我不認爲你需要在這兩個網絡申報的數據源。 xml和server.xml。嘗試刪除web.xml,如果它仍然不運行

+0

作爲一個紳士+1:D – JustDanyul 2013-04-11 11:30:10

+0

工作正常,沒有'web.xml'配置代碼。謝謝你們。 – azendh 2013-04-11 11:41:06

2

hibernate.cfg.xml中,嘗試用

<property name="hibernate.connection.datasource">java:/comp/env/jdbc/connectorDs</property> 
+0

擁有10s;)+1 so – Gab 2013-04-11 11:27:49