2012-05-10 1 views
1

저는 자바 초보자입니다. JTA 트랜잭션을 사용하여 지속 유닛을 구성하는 데 어려움이 있습니다. 이미 정의되고, 설정되고 채워지는 PostgreSQL 데이터베이스를 사용해야합니다. 넷빈즈를 사용하여, 나는 휴한지로 persistance.xmlglassfish-resources.xml 생성 :@PersistenceUnit을 사용하여 EntityManagerFactory를 주입하지 않습니다.

<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="WellWatcherPU" transaction-type="JTA"> 
     <jta-data-source>WellWatcherDB</jta-data-source> 
     <exclude-unlisted-classes>false</exclude-unlisted-classes> 
     <properties> 
      <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/> 
      <property name="eclipselink.logging.level" value="FINE"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

<resources> 
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.postgresql.ds.PGSimpleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="post-gre-sql_geowellex_geowellexPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"> 
     <property name="serverName" value="localhost"/> 
     <property name="portNumber" value="5432"/> 
     <property name="databaseName" value="DBNAME"/> 
     <property name="User" value="USER"/> 
     <property name="Password" value="PASSWORD"/> 
     <property name="URL" value="jdbc:postgresql://localhost:5432/DBNAME"/> 
     <property name="driverClass" value="org.postgresql.Driver"/> 
    </jdbc-connection-pool> 
    <jdbc-resource enabled="true" jndi-name="WellWatcherDB" object-type="user" pool-name="post-gre-sql_geowellex_geowellexPool"/> 
</resources> 

을 그리고 이것은 내가의 EntityManagerFactory와 EntityManager에 (넷빈즈 예에서 사용되는)

public class EUserDao { 

@Resource 
private UserTransaction utx = null; 
@PersistenceUnit(unitName = "WellWatcherPU") 
private EntityManagerFactory emf = null; 

public EntityManager getEntityManager() { 
    return emf.createEntityManager(); <-------- NullPointerException here 
} 

public EUser getOne(long userId){ 
    EntityManager em = getEntityManager(); 
    try { 
     return em.find(EUser.class, userId); 
    } finally { 
     em.close(); 
    } 
} 
을 얻는 방법이다

편집 :

여기 내 글라스가 있습니다. sfish 배포 로그 :

정보 : [EL Config] : 2012-05-10 12 : 01 : 13.534 - ServerSession (2017352940) - 연결 (1901223982) - 스레드 (스레드 [admin-thread-pool-4848 (5) (5), 그리즐리 커널]) - 연결 (DatabaseLogin ( 플랫폼 => DatabasePlatform 사용자 이름 = "" " 커넥터 => JNDIConnector 데이터 소스 이름 => 널 ))

Informações : EL 구성] : 2012-05-10 12 : 01 : 13.534 - ServerSession (2017352940) - 연결 (1462281761) - 스레드 (관리 쓰레드 - 풀 - 4848 (5), 5, 그리즐리 커널) -Connected : jdbc : postgresql : // localhost : 5432/geowellex? loginTimeout = 0 & prepareThreshold = 0 사용자 : g eowellex 데이터베이스 : PostgreSQL의 버전 : 9.1.3이 드라이버 : PostgreSQL의 기본 드라이버 버전 : PostgreSQL의 8.3 JDBC3 SSL과 ((603)를 구축)

Informações : [EL 구성] : 2012-05-10 12 : 01 : 13.534-- (DatabaseLogin ( 플랫폼 => PostgreSQLPlatform 사용자 이름 => 서버 세션) - 연결 (766700859) - 연결 (766700859) - 스레드 (스레드 [관리자 - 스레드 풀 4848 (5), 5, 그리즐리 커널] "" 커넥터 => JNDIConnector 데이터 소스 이름 => null ))

무엇이 잘못 되었습니까?

+0

엔티티 관리자 팩토리의 초기화와 관련된 항목을 웹 서버 로그에서 게시하십시오. – Perception

+0

방금 ​​수정되었습니다. 도와 줘서 고마워. –

답변

3

EUserDao는 정규 수업 일 가능성이 가장 큰 문제입니다. 주입은 컨테이너 관리 클래스에서만 작동합니다. @PersistenceUnit 및 @Resource와 같은 특수 효과는 일반 클래스에서 처리되지 않습니다.클래스의 유형에 따라

컨테이너 관리 클래스입니다 (그 @PersistenceUnit에서 사용할 수 있습니다) :

  • 서블릿 : 서블릿, 서블릿 필터, 이벤트 리스너
  • JSP : 태그 핸들러, 태그 라이브러리 이벤트 리스너
  • JSF : 범위 관리 빈
  • JAX-WS : 서비스 엔드 포인트, 핸들러
  • EJB
  • : 콩, 인터셉터
  • 관리 콩 : 관리 콩
  • CDI : CDI 스타일 관리 콩, 장식
  • 자바 EE 플랫폼 : 기본 클래스 (정적), 로그인 콜백 핸들러
+0

이제부터는이 튜토리얼에서 Dao를 SessionBean으로 정의했다. 곧 그 결과를 확인하고 확인할 것입니다. –

+0

고마워요. 나는 Dao가 ManagedBean이어야 함을 보지 못했습니다. –

+0

이 목록은 어디에 기록되어 있습니까? –

1

나는 볼 당신의 코드 선언에서 :

private EntityManagerFactory emf = null; 

그러나 결코 당신이 얻을 이유

emf = Persistence.createEntityManagerFactory("WellWatcherPU"); 

그게처럼 ... 하나를 만들 개체를 사용할 때 Null 포인터 예외!

public EntityManager getEntityManager() { 
    return emf.createEntityManager(); <-------- NullPointerException here 
}