0

2 つの tomcat サーバー (tcat01 と tcat02) を負荷分散アーキテクチャに配置する必要があります。

Tomcat 6.x を使用しており、tomcat tcat01 で次のように conf/server.xml を編集しました。

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tcat01">
     <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"/>
      ....

tcat02 では、conf/server.xml は次のようになります。

 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tcat02">
     <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"/>
      ....

tcat01 を開始し、次に tcat02 を開始しました。 il catalina.out tcat01 は tcat02 とうまく通信しているようです。

次に、インターネットナビゲーターを使用して webapp に接続し、webapp で何かを行うたびに (ナビゲートするとき)、次の例外が発生します。

  Nov 24, 2011 12:00:13 AM org.apache.catalina.core.ContainerBase backgroundProcess
WARNING: Exception processing manager org.apache.catalina.ha.session.DeltaManager@278c4835 background process
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mycompany.myproject.model.authentification.Authority.groups, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:332)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)

そして、これが sezialize できないクラスのコードです (つまり、スタック トレースで言及されている Java Bean):

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Entity
@Table(name = "authority", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Authority implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = -7436593455923433675L;

//SYSTEM ROLE CONSTANT
public static final String MANAGER_SYSTEM_ROLE = "ROLE_MANAGER";
public static final String CLIENT_SYSTEM_ROLE = "ROLE_CLIENT";
public static final String PEDAGO_ADMIN_SYSTEM_ROLE = "ROLE_PEDAGO_ADMIN";

private Integer id;
@Size(min=1)
@Pattern(regexp="^ROLE[_a-zA-Z]+$", message="{authority.name.pattern.error}")
private String name;
@Size(max=65535)
private String description;


private Boolean isSystem;

private Set<Group> groups = new HashSet<Group>(0);

public Authority() {
    this.isSystem = false;
}

public Authority(String name) {
    this.name = name;
    this.isSystem = false;
}

public Authority(String name, String description, Set<Group> groups) {
    this.name = name;
    this.description = description;
    this.groups = groups;
    this.isSystem = false;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@Column(name = "name", unique = true, nullable = false, length = 45)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name = "description")
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public void setIsSystem(Boolean isSystem) {
    this.isSystem = isSystem;
}

@Column(name = "system")
public Boolean getIsSystem() {
    return isSystem;
}

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "group_authorities", joinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "group_id", nullable = false, updatable = true) })
public Set<Group> getGroups() {
    return this.groups;
}

public void setGroups(Set<Group> groups) {
    this.groups = groups;
}

@PrePersist
protected void updateSystemField() {
    if(isSystem == null)
        isSystem = false;
}
}

次に、Java Bean グループのコードを示します (グループのコレクションに遅延初期化例外があるため)。

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


@Entity
@Table(name = "groups", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Group implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 7380068327050752558L;

private Integer id;
@NotNull
@Size(min=1)
private String name;
private String description;

private Boolean isSystem;

private Set<Authority> authorities = new HashSet<Authority>(0);
private Set<User> users = new HashSet<User>(0);

public Group() {
    this.isSystem = false;
}

public Group(String name) {
    this.name = name;
    this.isSystem = false;
}

public Group(String name, String description, Set<Authority> authorities, Set<User> users) {
    this.name = name;
    this.description = description;
    this.isSystem = false;
    this.authorities = authorities;
    this.users = users;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@Column(name = "name", unique = true, nullable = false, length = 45)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name = "description")
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public void setIsSystem(Boolean isSystem) {
    this.isSystem = isSystem;
}

@Column(name = "system")
public Boolean getIsSystem() {
    return isSystem;
}

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "group_authorities", joinColumns = { @JoinColumn(name = "group_id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = true) })
public Set<Authority> getAuthorities() {
    return this.authorities;
}

public void setAuthorities(Set<Authority> authorities) {
    this.authorities = authorities;
}

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "group_users", joinColumns = { @JoinColumn(name = "group_id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = true) })
public Set<User> getUsers() {
    return this.users;
}

public void setUsers(Set<User> users) {
    this.users = users;
}

public void addAuthority(Authority authority) {
    if(authorities == null)
        authorities = new HashSet<Authority>(0);
    authorities.add(authority);
}

public boolean removeAuthority(Authority authority) {
    return authorities == null || authorities.remove(authority);
}

public void addUser(User user) {
    if(users == null)
        users = new HashSet<User>(0);
    users.add(user);
}

public boolean removeUser(User user) {
    return users == null || users.remove(user);
}

@PrePersist
protected void updateSystemField() {
    if(isSystem == null)
        isSystem = false;
}
}

ご協力いただきありがとうございます

4

1 に答える 1

0

権限グループが遅延としてマップされているため、遅延インスタンス化例外が発生しています。セッションがレプリケートされるまでには、アクティブなセッションの外にあるため、ハイドレートできません。これを解決するには、いくつかのオプションがあります。

  1. 関連付けが積極的に取得されるようにマッピングを変更します
  2. DAO を変更してそのコレクションにステップインする (または hibernate.initialize を使用する) ことで、セッションが閉じられる前に遅延ロードが強制的に発生するようにします。
  3. オブジェクトをセッションから切り離し、オブジェクトをセッションに入れる前に、オブジェクトのグループを明示的に null に設定して、そこにある休止状態のプロキシを置き換えます。

この特定のケースでは、オプション 1 または 2 がおそらく最もクリーンです。

于 2011-11-30T17:31:23.767 に答える