使用mod_proxy_ajp连接Apache和Tomcat可以正常工作,但JDBC会失败

时间:2012-06-29 23:45:00

标签: apache tomcat jdbc mod-proxy ajp

我在Debian Squeeze上运行Apache 2.2.16,它使用不同的域(例如phpApp1.com,phpApp2.com)为/ var / www /提供一些PHP应用程序。

我还有一个有效的Tomcat 7.x,它位于/ opt / tomcat /并且服务于一个JSF + JPA应用程序,它位于/ opt / tomcat / webapps / MyApp /.

因此可以通过以下方式访问MyApp: myapp.com:8080/MyApp/home.html或phpApp1.com:8080/MyApp/home.html(因为端口)。由于我希望这是myapp.com/home.html,我使用mod_proxy_ajp使用以下配置文件连接Apache和Tomcat:

/ etc / apache2 / sites-available / MyApp

中的Apache虚拟主机
<Virtualhost *:80>
    ServerName myapp.com
    ServerAlias *.myapp.com
    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
</Virtualhost>

和/opt/tomcat/conf/server.xml中的T​​omcat虚拟主机

<Host name="myapp.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Context path="" reloadable="true" debug="0" docBase="MyApp" />
    <Alias>www.myapp.com</Alias>
</Host>

这就是我可以访问http://myapp.com/home.html并获得正确的html页面,包括css,images和js。所以tomcat中的AJP连接器应该可以工作。

但是应用程序无法建立JDBC连接,否则可以正常工作。无论我使用的是myapp.com还是myapp.com:8080,都无论如何都会失败。但是,如果我通过http://phpapp1.com:8080/MyApp/home.html访问我的应用程序,它就有效。它只在我使用代理域“myapp.com”时失败。

使用AJP和JDBC时是否有人遇到过类似的问题?我怎么解决这个问题?使用mod_jk时我遇到了同样的问题,它也使用了AJP。提前谢谢。

更新1

@Mark Rotteveel:“直接连接”是什么意思?我的意思是如果我在tomcats server.xml中使用未列为虚拟主机的域之一,则JDBC连接正在工作。

这是连接错误:

ERROR: HHH000231: Schema export unsuccessful
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:492)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.myapp.util.database.Database.<init>(Database.java:70)
at com.myapp.util.database.Database.<clinit>(Database.java:38)
at com.myapp.controller.file.UploadBacking.ajaxUpload(UploadBacking.java:89)
at com.myapp.controller.file.UploadBacking$Proxy$_$$_WeldClientProxy.ajaxUpload(UploadBacking$Proxy$_$$_WeldClientProxy.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(ContextAwareTagMethodExpression.java:96)
at org.apache.myfaces.view.facelets.tag.jsf.core.AjaxHandler$AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxHandler.java:495)
at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:57)
at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:74)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:405)
at javax.faces.component.UICommand.broadcast(UICommand.java:103)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:1028)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:286)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1375)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:38)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:183)
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:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
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.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:200)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 60 more

来自Database.java:

private Database() {
    entityManagerFactory = Persistence.createEntityManagerFactory("myapp_persistence_unit");
}

MyApp的/ META-INF / context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Context>

<Context>

<Resource
    name="jdbc/db_myapp" 
    auth="Container"
    type="javax.sql.DataSource" 
    username="user_myapp" 
    password="##########"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/db_myapp?autoReconnect=true"
    validationQuery="select 1"
    maxActive="15" 
    maxIdle="3" />

<Valve
    className="org.apache.catalina.valves.AccessLogValve"
    directory="logs/myapp"
    prefix="myapp_access_"
    suffix=".log"
    pattern="%h %l %u %t &quot;%r&quot; %s %b"
    resolveHosts="false" />

</Context>

来自WEB-INF / web.xml

<resource-ref>
    <description>MySQL database</description>
    <res-ref-name>jdbc/db_myapp</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

最后是WEB-INF / classes / META-INF / persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="myapp_persistence_unit">
        <properties>
            <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/db_myapp" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.current_session_context_class" value="thread" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>

</persistence>

2 个答案:

答案 0 :(得分:3)

我终于开始工作了。如果您在tomcat中创建虚拟主机,则必须将应用程序/META-INF/context.xml的完全副本放入/tomcat/conf/Catalina/domain.com/并将其命名为context.xml.default。好吧,我不知道是否总是如此,但对于我的设定它是。所以这些是我当前的配置文件来完成这项工作:

/etc/apache2/sites-enabled/domain.com

<Virtualhost *:80>
    ServerName domain.com
    ServerAlias *.domain.com

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass / ajp://my.server.ip:8009/
</Virtualhost>

/tomcat/conf/server.xml

<Host name="domain.com">
    <Context path="" docBase="MyApp" />
    <Alias>www.domain.com</Alias>
</Host>

我从2006年开始在coderanch.com上阅读this thread后尝试了这一点(作者的解决方案在最后一篇文章中)。我没有更改web.xmlcontext.xml,因为这不是<Resource>元素的错误配置。无论如何,感谢您尝试提供帮助:)

答案 1 :(得分:0)

确保您的mysql-connector-j.x.y.z.jar文件只在一个位置:Tomcat的lib目录。确保您的任何网络应用中都没有“迷你副本”WEB-INF/lib directories

两个不相关的提示:

  1. 请勿使用autoReconnect
  2. 使用/* ping */ SELECT 1作为验证查询:Connector / J的最新版本将使用轻量级“ping”而不是实际发出查询以测试连接的有效性