我有一个使用Tomcat和Hibernate + c3p0的完美的webapp。 我以编程方式配置hibernate,使用getClass()。getResource(/ a/b/model)加载模型类并添加注释类。getClass()。getResource + Tomcat + Hibernate
现在,我被要求更改c3p0并改用Tomcat JDBC连接池。我跟着几篇文章,显然一切正常。现在的问题是,我在尝试加载模型类并配置hibernate时遇到错误。
配置hibernate(HibernateConnector.class)的类位于webapps \ MyApp \ WEB-INF \ lib \ MyJar.jar中的jar(MyJar)中。
如果我做的getClass()的getResource( “HibernateConnector.class”)我得到正确的路径: JAR:文件:/ C:/apache-tomcat-7.0.29/webapps/MyApp/WEB-INF/ lib/MyJar.jar!/a/b/c/HibernateConnector.class
因为当我询问getClass()。getResource(/ a/b/model)时,我删除了c3p0并配置了Tomcat连接池,所以我得到file: /C:/apache-tomcat-7.0.29/work/Catalina/localhost/MyApp/loader/a/b/model
我没有碰到关于加载的任何代码,所以显然有些奇怪的事情正在发生新的配置。有没有人有如何解决这个问题的想法?为什么这条道路正在改变工作/卡塔利娜....?
对不起,长期发帖,并感谢提前!
下面是我使用Tomcat连接池所做的更改。 在web.xml中我补充:
<resource-ref>
<description>This is a MySQL database connection</description>
<res-ref-name>jdbc/myDb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
的context.xml:
<Context antiJARLocking="true" path="MyApp">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="30" maxIdle="10" maxWait="10000" name="jdbc/myDb" password="pass"
type="javax.sql.DataSource" url="jdbc:mysql://localhost/myDb" username="user"/>
</Context>
和配置Hibernate的编程方式:
props.put("hibernate.connection.datasource", "java:comp/env/jdbc/myDb");
props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");