我们可以在AWS Lambda程序中使用hibernate来连接相关数据库吗?

时间:2016-03-23 11:31:03

标签: hibernate amazon-web-services aws-lambda

我编写了一个AWS Lambda函数(像往常一样实现了handleRequest,没有数据库连接工作正常),它实际上假设与亚马逊托管的Oracle数据库连接。

对于同样的我已经添加了Hibernate Util和相关的配置文件来连接数据库。因此,在调用AWS Lambda函数时,会将特定记录插入到数据库中。

但我收到以下错误,我在这里缺少什么?

{
  "errorMessage": "Could not initialize class com.exercise.HibernateUtil",
  "errorType": "java.lang.NoClassDefFoundError",
  "stackTrace": [
    "com.exercise.CurrentTime.handleRequest(CurrentTime.java:34)",
    "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
    "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
    "java.lang.reflect.Method.invoke(Method.java:497)"
  ]
}

修改

我发现hibernate.cfg.xml没有被添加,后来我添加了所有必需的文件,但最后它给出了以下问题[一切都在我的桌面上运行良好]:

我的问题是,我们有任何基于hibernate的包上传为lambda函数或AWS lambda服务意味着最小逻辑功能的示例。

START RequestId: dcece63c-f0e8-11e5-9f50-edb8ba93d932 Version: $LATEST 
First Log :Type:Notification, requestId:dcece63c-f0e8-11e5-9f50-edb8ba93d932, function:simple-lambda-java-exercise, groupName:/aws/lambda/simple-lambda-java-exercise, logStreamName:2016/03/23/[$LATEST]6413c2ef43b14e1aad006f31ca92f260
Second Log :InputMessage :sample message
Maven + Hibernate + Oracle 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
SLF4J: Defaulting to no-operation (NOP) logger implementation 
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 
Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found 
java.lang.ExceptionInInitializerError: java.lang.ExceptionInInitializerError java.lang.ExceptionInInitializerError at com.exercise.HibernateUtil.buildSessionFactory(HibernateUtil.java:17) at com.exercise.HibernateUtil.<clinit>(HibernateUtil.java:8) at com.exercise.CurrentTime.handleRequest(CurrentTime.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2176) at org.hibernate.cfg.Configuration.configure(Configuration.java:2157) at org.hibernate.cfg.Configuration.configure(Configuration.java:2137) at com.exercise.HibernateUtil.buildSessionFactory(HibernateUtil.java:13) ... 6 more  
END RequestId: dcece63c-f0e8-11e5-9f50-edb8ba93d932 
REPORT RequestId: dcece63c-f0e8-11e5-9f50-edb8ba93d932  Duration: 1368.02 ms    Billed Duration: 1400 ms Memory Size: 512 MB    Max Memory Used: 43 MB   
START RequestId: e798ae13-f0e8-11e5-a603-69c02723850a Version: $LATEST 
First Log :Type:Notification, requestId:e798ae13-f0e8-11e5-a603-69c02723850a, function:simple-lambda-java-exercise, groupName:/aws/lambda/simple-lambda-java-exercise, logStreamName:2016/03/23/[$LATEST]6413c2ef43b14e1aad006f31ca92f260
Second Log :InputMessage :sample message
Maven + Hibernate + Oracle 
Could not initialize class com.exercise.HibernateUtil: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: Could not initialize class com.exercise.HibernateUtil at com.exercise.CurrentTime.handleRequest(CurrentTime.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497)  
END RequestId: e798ae13-f0e8-11e5-a603-69c02723850a 
REPORT RequestId: e798ae13-f0e8-11e5-a603-69c02723850a  Duration: 1.26 ms   Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 43 MB   
START RequestId: e9116b9c-f0e8-11e5-bccb-4f6d3966e31d Version: $LATEST 
First Log :Type:Notification, requestId:e9116b9c-f0e8-11e5-bccb-4f6d3966e31d, function:simple-lambda-java-exercise, groupName:/aws/lambda/simple-lambda-java-exercise, logStreamName:2016/03/23/[$LATEST]6413c2ef43b14e1aad006f31ca92f260
Second Log :InputMessage :sample message
Maven + Hibernate + Oracle 
Could not initialize class com.exercise.HibernateUtil: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: Could not initialize class com.exercise.HibernateUtil at com.exercise.CurrentTime.handleRequest(CurrentTime.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497)  
END RequestId: e9116b9c-f0e8-11e5-bccb-4f6d3966e31d 
REPORT RequestId: e9116b9c-f0e8-11e5-bccb-4f6d3966e31d  Duration: 1.25 ms   Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 43 MB   
START RequestId: 2d150ad7-f0ec-11e5-afb5-e3e30ad68aea Version: $LATEST 
First Log :Type:Notification, requestId:2d150ad7-f0ec-11e5-afb5-e3e30ad68aea, function:simple-lambda-java-exercise, groupName:/aws/lambda/simple-lambda-java-exercise, logStreamName:2016/03/23/[$LATEST]6413c2ef43b14e1aad006f31ca92f260
Second Log :InputMessage :sample message
Maven + Hibernate + Oracle 
Could not initialize class com.exercise.HibernateUtil: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: Could not initialize class com.exercise.HibernateUtil at com.exercise.CurrentTime.handleRequest(CurrentTime.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497)  
END RequestId: 2d150ad7-f0ec-11e5-afb5-e3e30ad68aea 
REPORT RequestId: 2d150ad7-f0ec-11e5-afb5-e3e30ad68aea  Duration: 1.24 ms   Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 43 MB   

1 个答案:

答案 0 :(得分:0)

您的jar中没有包含库,运行时需要。您将需要具有所有依赖项的jar。考虑使用Maven Shade插件或Maven组件插件,并验证您的jar是否包含所有必需的依赖项,然后上载此jar。它应该工作。

稍后,您可以考虑将AWS层用于依赖​​关系,这样就不必将jar与依赖关系捆绑在一起。