我在基于struts框架2.3.3和tomcat 7.0.26的项目中使用hibernate 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:找不到合适的jdbc驱动程序:mysql:// localhost / struts2 在java.sql.DriverManager.getConnection(DriverManager.java:604)
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。
提前致谢
答案 0 :(得分:1)
当发生这种事情(“找不到合适的驱动程序”)时,通常的问题是你的JDBC库要么位于错误的位置(取决于你的要求),要么位于太多的地方(例如WEB-INF / lib以及容器的lib /目录中。)
哪个组件实际上是在创建JDBC连接池?如果它是Tomcat(似乎不是这种情况),那么你的mysql.jar需要在Tomcat的lib /目录中(特别是不在你的webapp的WEB-INF / lib中)。如果您正在webapp中执行所有操作,那么您可能希望mysql.jar位于webapp中的WEB-INF / lib和/或Tomcat的lib目录中。
如果您有多个webapps并且其中一些使用Tomcat的DataSource配置,那么最好将mysql.jar放入Tomcat的lib /目录中,并将其从所有webapps的WEB-INF / lib目录中删除。