我有一个用户登录的JSF 2.0页面,他可以选择退出(令人惊讶)。我的JBoss服务器配置允许最多7个线程(连接)。我用一个用户测试了多次登录页面,在第7次尝试之后我得到Transaction not active
这可能意味着在注销后连接不会回到池中并保持打开状态。
问:在线程池中注销并返回线程的方法是什么?这个问题折磨了我很久。请帮忙。
以下是JBoss standalone.xml
中用于限制连接的数据源的配置:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/MyJndiDS" pool-name="MyPoolDS" enabled="true" jta="true" use-java-context="false" use-ccm="true">
<connection-url>
jdbc:postgresql://192.168.2.125:5432/t_report
</connection-url>
<driver>
org.postgresql
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
3
</min-pool-size>
<max-pool-size>
7
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
my_user
</user-name>
<password>
my_pass
</password>
</security>
<statement>
<prepared-statement-cache-size>
32
</prepared-statement-cache-size>
</statement>
</datasource>
...
...
</datasources>
</subsystem>
和@SessionScoped
类
import javax.faces.context.ExternalContext;
...
...
@Inject ExternalContext ec;
public void validateUserLogOut() {
HttpServletRequest request = (HttpServletRequest)ec.getRequest();
request.getSession().invalidate();
this.setUserLoggedIn(false);
navigation.logout();
}
编辑:以下是用户登录的方式。希望这会有所帮助。
public void validateLogUser() {
ResourceBundle bundle = ResourceBundle.getBundle("internationalization.language", context.getViewRoot().getLocale());
String validation = logUser();
if((validation == null) || validation.isEmpty()) {
context.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
bundle.getString("wrongUsername"),bundle.getString("wrongUsername")));
} else if (validation == "welcome") {
this.setUserLoggedIn(true);
navigation.login();
}
}
logUser()
所在的位置:
public synchronized String logUser() {
try {
EntityManagerUtil.getEntityManager().getTransaction().begin();
System.out.println(user);
if(user.getUsername().isEmpty() || (user.getUsername() == null)) {
return null;
}
String password = user.getPassword();
user = (UserBean) EntityManagerUtil.getEntityManager().find(UserBean.class, user.getUsername());
if(user == null) {
HttpServletRequest request = (HttpServletRequest)ec.getRequest();
request.getSession().invalidate();
}
if(user.getPassword().equals(password)) {
log.info("User: " + user.getUsername() + " logged successfully.");
return "welcome";
} else {
HttpServletRequest request = (HttpServletRequest)ec.getRequest();
request.getSession().invalidate();
return null;
}
} catch (Exception e) {
log.error("Error while logging in : \n\t" + e);
EntityManagerUtil.getEntityManager().getTransaction().rollback();
return null;
} finally {
EntityManagerUtil.close();
}
}
这就是EntityManagerUtil.getEntityManager()
的工作原理:
/**
* ThreadLocal instance that holds unique EntityManager per thread,
* it means that every thread accessing this ThreadLocal will has it's own instance of EntityManager
*/
private static final ThreadLocal<EntityManager> entitymanager =
new ThreadLocal<EntityManager>();
/**
* @param persistenceUnit - String name of the persistece unit
* to be used as declared inside persistence.xml
* @return singleton instance of EntityManagerFactory
*/
public synchronized static EntityManagerFactory initializeEntityManagerFactory( String persistenceUnit ) {
if ( entityManagerFactory == null ) {
// Create the EntityManagerFactory
entityManagerFactory = Persistence.createEntityManagerFactory( persistenceUnit );
}
return entityManagerFactory;
}
/**
* @return Singleton instance of EntityManager per Thread
*/
public static EntityManager getEntityManager() {
initializeEntityManagerFactory( "MyPersistenceUnit" );
EntityManager entityManager = entitymanager.get();
// Create a new EntityManager
if ( entityManager == null) {
entityManager = entityManagerFactory.createEntityManager();
entitymanager.set( entityManager );
}
return entityManager;
}
/**
* Close all ThreadLocals
*/
public static void close() {
final EntityManager entityManager = entitymanager.get();
entitymanager.set( null );
if ( entityManager != null && entityManager.isOpen()) {
entityManager.close();
}
if ( entityManagerFactory != null && entityManagerFactory.isOpen()) {
entityManagerFactory.close();
}
}
答案 0 :(得分:4)
你正在重新发明轮子。请使用容器管理实体管理器注入@PersistenceContext
注释和EJB
来管理事务。它会更容易,也不容易出错。
这是一个简单的DAO:
@Stateless
public class UserDAO {
@PersistenceContext
private EntityManager entityManager;
public void insertUser(User user) {
entityManager.persist(user);
}
}
默认情况下,EJB
中的每个方法都是事务性的。您可以使用@TransactionAttribute
注释实现细粒度控制。这很简单。