我正在使用 Netbeans 7.3、带有 Primefaces 3.5 和 Hibernate 3.2.5 的 JSF 2.1 创建一个 Web 应用程序。我在 glassfish 服务器 3.1.2 上运行它 我的项目有一些维护(我不确定如何用英语调用它,我说的是一个可以创建、更新和删除数据的屏幕),因为一:
(我把一些原来的名字和文字从西班牙语改成了英语)
MANOPINIONES 观点:
<f:view locale="#{sesion.idiomaActual}">
<h:head>
<link href="CSS/estilos.css" rel="stylesheet" type="text/css"/>
</h:head>
<h:body rendered="#{sesion.isLogadoAdm()}">
<h:form>
<div class="area_mantenimiento">
<br/>
<p:panel id="panelopi" header="Mantenimiento de opiniones" styleClass="panel_mantenimiento_opi">
<p:messages globalOnly="true"/>
<br/>
<p:toolbar>
<p:toolbarGroup align="left">
<p:commandButton title="Ok" icon="ui-icon-check" disabled="#{Manopiniones_BackingBean.disabled_ok}" actionListener="#{Manopiniones_BackingBean.saveChangesListener(event)}" update="panelopi"/>
<p:commandButton title="Cancel" icon="ui-icon-closethick" disabled="#{Manopiniones_BackingBean.disabled_cancel}" actionListener="#{Manopiniones_BackingBean.cancelChangesListener(event)}" update="panelopi"/>
<p:commandButton title="Delete" icon="ui-icon-circle-minus" type="button" disabled="#{Manopiniones_BackingBean.disabled_del}" onclick="vardialogoopi.show();"/>
</p:toolbarGroup>
</p:toolbar>
<br/><br/>
<p:panel>
<p:panelGrid columns="3" styleClass="rejilla_panel_login" style="margin:0 0 0 0;font: 10px Verdana;">
<h:outputText value="Codigo de opinion:" style="font-weight:bold;"/>
<p:inputText value="#{Manopiniones_BackingBean.codigo}" disabled="#{Manopiniones_BackingBean.disabled_cod}" />
<p:commandButton title="Buscar" value=" " icon="ui-icon-search" disabled="#{Manopiniones_BackingBean.disabled_bot}" actionListener="#{Manopiniones_BackingBean.getOpinionListener(event)}" update="panelopi"/>
</p:panelGrid>
</p:panel>
<p:separator style="width:580px;height:10px; margin:10px auto auto 0;" />
<br/>
<p:panel>
<p:panelGrid columns="2" styleClass="rejilla_panel_login" style="margin:0 0 0 0;font: 10px Verdana;">
<h:outputText value="Texto: " style="font-weight:bold;"/>
<p:editor value="#{Manopiniones_BackingBean.opinion_actual.opiDes}" maxlength="5000" height="150" width="450" disabled="#{Manopiniones_BackingBean.disabled_opi}"/>
<h:outputText value="" />
<h:outputText value="" />
</p:panelGrid>
</p:panel>
<p:confirmDialog message="¿Are you sure to delete this?" header="Confirmacion" severity="alert" widgetVar="vardialogoopi" >
<p:commandButton value="OK" actionListener="#{Manopiniones_BackingBean.deleteOpinionListener(event)}" update="panelopi" oncomplete="vardialogoopi.hide();" />
<p:commandButton value="Cancel" type="button" onclick="vardialogoopi.hide();" />
</p:confirmDialog>
</p:panel>
</div>
</h:form>
</h:body>
</f:view>
Manopiniones 豆:
@ManagedBean
@ViewScoped
public class Manopiniones_BackingBean {
private Session sesion=null;
private Opiniones opinion_actual;
private Boolean disabled_ok=true;
private Boolean disabled_cancel=true;
private Boolean disabled_del=true;
private Boolean disabled_cod=false;
private Boolean disabled_bot=false;
private Boolean disabled_opi=true;
private String codigo="";
public Manopiniones_BackingBean() {
}
@PreDestroy
public void parar() {
if(sesion!=null && sesion.isOpen()){
sesion.getTransaction().rollback();
sesion.close();
opinion_actual=new Opiniones();
disabled_ok=true;
disabled_cancel=true;
disabled_del=true;
disabled_cod=false;
disabled_bot=false;
disabled_opi=true;
}
}
public Session getSesion() {
return sesion;
}
//GETTER & SETTERS OMITTED
public void saveChangesListener(ActionEvent event) {
try {
if(opinion_actual!=null && !codigo.isEmpty()){
opinion_actual.setOpiCod(codigo);
sesion.saveOrUpdate(opinion_actual);
sesion.getTransaction().commit();
sesion.close();
opinion_actual=new Opiniones();
disabled_ok=true;
disabled_cancel=true;
disabled_del=true;
disabled_cod=false;
disabled_bot=false;
disabled_opi=true;
};
}
catch (HibernateException he) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error saving opinion.", null));
sesion.getTransaction().rollback();
sesion.close();
};
}
public void cancelChangesListener(ActionEvent event) {
sesion.getTransaction().rollback();
sesion.close();
opinion_actual=new Opiniones();
disabled_ok=true;
disabled_cancel=true;
disabled_del=true;
disabled_cod=false;
disabled_bot=false;
disabled_opi=true;
}
public void deleteOpinionListener(ActionEvent event) {
try {
sesion.delete(opinion_actual);
sesion.getTransaction().commit();
sesion.close();
opinion_actual=new Opiniones();
codigo="";
disabled_ok=true;
disabled_cancel=true;
disabled_del=true;
disabled_cod=false;
disabled_bot=false;
disabled_opi=true;
}
catch (HibernateException he) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error deleting opinion.", null));
sesion.getTransaction().rollback();
sesion.close();
};
}
public void getOpinionListener(ActionEvent event) {
try {
if(!codigo.isEmpty()){
sesion=HibernateUtil.getSessionFactory().openSession();
sesion.beginTransaction();
opinion_actual=(Opiniones)sesion.get(Opiniones.class, codigo);
disabled_ok=false;
disabled_cancel=false;
disabled_cod=true;
disabled_bot=true;
disabled_opi=false;
disabled_del=false;
if(opinion_actual==null){
opinion_actual=new Opiniones();
disabled_del=true;
};
};
}
catch (HibernateException he) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error getting opinion.", null));
sesion.getTransaction().rollback();
sesion.close();
};
}
}
如您所见,它是一个非常简单的“维护”示例,并且使用原始配置可以完美运行。
我想使用 c3p0 库实现一个连接池以提高速度,所以我在hibernate.cfg.xml
休眠.CFG.XML:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/agencia</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">THE_PASSWORD</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.jdbc.batch_size">50</property>
**<!-- ADDED LINES TO IMPLEMENT CONNECTION POOL -->**
<property name="hibernate.c3p0.min_size">8</property>
<property name="hibernate.c3p0.max_size">32</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.connection.datasource">jdbc/PoolAgencia</property>
**<!-- ADDED LINES TO IMPLEMENT CONNECTION POOL -->**
**<!-- HIBERNATE MAPPING OMITTED -->**
</session-factory>
</hibernate-configuration>
我还在 glassfish 服务器上创建了一个连接池。我下载并添加mysql-connector-java-5.0.8-bin
到我的服务器。要配置池,我刚刚更改了这些可选属性:
Databasename = the_database_name
Password = the_password
Portnumber = 3306
Servername = localhost
User = root
最后我创建了一个名为jdbc/PoolAgencia
.
它似乎运作良好,在网络的其他点检索数据非常快。然后我以这种方式使用保养:
我去了“保养”页面。
我介绍了一个代码
我按下了“搜索”按钮。
然后我按下“取消”按钮,我得到这个错误:
SEVERE: JDBC rollback failed
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Can't call rollback when autocommit=true
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.rollback(Connection.java:5257)
at com.sun.gjc.spi.base.ConnectionHolder.rollback(ConnectionHolder.java:630)
at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:183)
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:162)
如果我以这种方式使用“保养”:
- 我进入“维护”页面。
- 我介绍一个代码(不管是新的还是现有的)
- 我按下“搜索”按钮。(如果代码存在于数据库中,则检索它,否则会创建一个新代码)
- 然后我按下“确定”按钮(应该保存所有更改),我得到这个错误:
SEVERE: JDBC commit failed
java.sql.SQLException: Can't call commit when autocommit=true
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:914)
at com.mysql.jdbc.Connection.commit(Connection.java:2275)
at com.sun.gjc.spi.base.ConnectionHolder.commit(ConnectionHolder.java:244)
at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:139)
当我阅读这两个错误时,我了解到事务无法在池中正常工作。我认为这是连接池的问题或休眠配置问题。我在网上进行了搜索,并尝试更改连接池的一些配置选项,但没有运气。
为什么会这样?