我在基于 struts 框架 2.3.3 和 tomcat 7.0.26 的项目中使用休眠 4.1.2。我也在使用 mysql(和 postgres)进行测试。这是我使用 MySql 的 hibernate.cfg.xml:
<session-factory>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">300</property>
<property name="c3p0.max_statements">50</property>
<property name="c3p0.idle_text_period">3000</property>
<property name="show_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/struts2</property>
<property name="connection.username">semperEst</property>
<property name="connection.password">*******</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>
<mapping class="firstname.secondname.lesson2.User"/>
</session-factory>
我的 WEB-INF/lib 包含:
antlr-2.7.7.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
dom4j-1.6.1.jar
freemarker-2.3.19.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.2.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.11.0.GA.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
log4j-1.2.16.jar
mysql-connector-java-5.1.20-bin.jar
ognl-3.0.5.jar
postgresql-9.1-902.jdbc4.jar
slf4j-api-1.6.4-sources.jar
slf4j-log4j12-1.6.4.jar
struts2-core-2.3.3.jar
xwork-core-2.3.3.jar
*我已经在 $CATALINA_HOME/lib 中添加了 mysql-connector-java-5.1.20-bin.jar*
但我仍然有这个例外:
java.sql.SQLException:在 java.sql.DriverManager.getConnection(DriverManager.java:604)找不到适合 jdbc:mysql://localhost/struts2 的驱动程序
at java.sql.DriverManager.getConnection(DriverManager.java:190)
at org.hibernate.service.jdbc.connections.internal.
DriverManagerConnectionProviderImpl.
getConnection(DriverManagerConnectionProviderImpl.java:192)
at org.hibernate.internal.
AbstractSessionImpl$NonContextualJdbcConnectionAccess.
obtainConnection(AbstractSessionImpl.java:278)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.
obtainConnection(LogicalConnectionImpl.java:297)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.
getConnection(LogicalConnectionImpl.java:169)
at org.hibernate.engine.transaction.internal.
jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.
AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.
beginTransaction(SessionImpl.java:1363)
at firstname.lastname.lesson2.
PortfolioService.usernameExists(PortfolioService.java:122)
at firstname.lastname.lesson2.CreateUser.validate(CreateUser.java:255)
at com.opensymphony.xwork2.validator.ValidationInterceptor.
doBeforeInvocation(ValidationInterceptor.java:251)
at com.opensymphony.xwork2.validator.ValidationInterceptor.
doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.
AnnotationValidationInterceptor.
doIntercept(AnnotationValidationInterceptor.java:68)
at
.
.
.(struts activation frame...)
.
at org.apache.catalina.core.ApplicationFilterChain.
internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.
doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.
invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.
invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.
invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.
invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.
process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.
process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.
run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
使用 postgres,同样的问题。一切都在一个独立的应用程序中工作,没有 struts 和 tomcat。
提前致谢