2011-11-02

從 Tomcat 5.0 升級到 Tomcat 6.0 時,設定 DataSource 出錯

設定如下:

hibernate.cfg.xml
<hibernate-configuration>
 <session-factory>
  <property name="connection.datasource">java:comp/env/jdbc/Pool</property>
  <property name="show_sql">true</property>
  <!-- mapping files -->
  <mapping resource="..." />
 </session-factory>
</hibernate-configuration>
tomcat/bin/conf/Catalina/localhost/Root.xml
<Context debug="9" displayName="..." docBase=".../webapp" path="/" reloadable="true" crossContext="true">
  <Loader className="org.apache.catalina.loader.WebappLoader" debug="9" reloadable="true"/>
  <Logger className="org.apache.catalina.logger.FileLogger" debug="9" prefix="tomcat." suffix=".log" timestamp="true"/>
  <Resource name="jdbc/Pool" type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/Pool">
    <parameter>
      <name>url</name>
      <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db;SelectMethod=Cursor</value>
    </parameter>
    <parameter>
      <name>maxIdle</name>
      <value>20</value>
    </parameter>
    <parameter>
      <name>maxActive</name>
      <value>40</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </parameter>
    <parameter>
      <name>maxWait</name>
      <value>5000</value>
    </parameter>
    <parameter>
      <name>username</name>
      <value>root</value>
    </parameter>
    <parameter>
      <name>password</name>
      <value>root</value>
    </parameter>
  </ResourceParams>
</Context>
執行時出錯:
org.apache.jasper.JasperException: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 ...
好吧,給你加上 Dialect!

元兇現身:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
 org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
 org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
 org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
 org.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:69)
 org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
 org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
 org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:129)
 org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
 org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1290)
 ...
最後發現,原來 Tomcat 5.5 以後的 DataSource 設定改變了,將 ResourceParams 併入 Resource 來簡化設定。
<Resource
 name="jdbc/Pool"
 type="javax.sql.DataSource"
 url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db;SelectMethod=Cursor"
 driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
 username="root"
 password="root"
 maxIdle="20"
 maxWait="5000"
 maxActive="40"/>

沒有留言:

張貼留言