如何在spring应用程序上下文中配置c3p0?
我正在运行mybatis + spring + c3p0 + Oracle 11g。
<{3}}的c3p0文档说:
<{3}}的extensions默认值:空java.util.Map java.util.Map(原始类型) 包含任何用户定义的配置扩展的值 为此DataSource定义。
c3p0文档说:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div data-role="page"> <div data-role="header" data-theme="f"> <a data-iconpos="notext" data-role="button" data-icon="home" title="Home">Home</a> <h1>BENG GPA Calculator</h1> <form id="form"> <ul data-role="listview" data-inset="true"> <li>Module 1</li> <li data-role="fieldcontain"> <label for="name1">Credits:</label> <input type="text" name="name1" id="name1" value="" data-clear-btn="true"> </li> <li data-role="fieldcontain"> <label for="grade1" class="select">Grade:</label> <select class="grade1" data-theme="f" id="grade1"> <option value="-1">—</option> <option value="4">A</option> <option value="3.7">A-</option> <option value="3.3">B+</option> <option value="3">B</option> <option value="2.7">B-</option> <option value="2.3">C+</option> <option value="2">C</option> <option value="1.7">C-</option> <option value="1.3">D+</option> <option value="1">D</option > <option value="0">F</option> </select> </li> <li>Module 2</li> <li data-role="fieldcontain"> <label for="name2">Credits:</label> <input type="text" name="name2" id="name2" value="" data-clear-btn="true"> </li> <li data-role="fieldcontain"> <label for="grade2" class="select">Grade:</label> <select class="grade2" data-theme="f" id="grade2"> <option value="-1">—</option> <option value="4">A</option> <option value="3.7">A-</option> <option value="3.3">B+</option> <option value="3">B</option> <option value="2.7">B-</option> <option value="2.3">C+</option> <option value="2">C</option> <option value="1.7">C-</option> <option value="1.3">D+</option> <option value="1">D</option> <option value="0">F</option> </select> </li> <li>Module 3</li> <li data-role="fieldcontain"> <label for="name3">Credits:</label> <input type="text" name="name3" id="name3" value="" data-clear-btn="true"> </li> <li data-role="fieldcontain"> <label for="grade3" class="select">Grade:</label> <select class="grade3" data-theme="f" id="grade3"> <option value="-1">—</option> <option value="4">A</option> <option value="3.7">A-</option> <option value="3.3">B+</option> <option value="3">B</option> <option value="2.7">B-</option> <option value="2.3">C+</option> <option value="2">C</option> <option value="1.7">C-</option> <option value="1.3">D+</option> <option value="1">D</option> <option value="0">F</option> </select> </li> <li>Module 4</li> <li data-role="fieldcontain"> <label for="name4">Credits:</label> <input type="text" name="name4" id="name4" value="" data-clear-btn="true"> </li> <li data-role="fieldcontain"> <label for="grade4" class="select">Grade:</label> <select class="grade4" data-theme="f" id="grade4"> <option value="-1">—</option> <option value="4">A</option> <option value="3.7">A-</option> <option value="3.3">B+</option> <option value="3">B</option> <option value="2.7">B-</option> <option value="2.3">C+</option> <option value="2">C</option> <option value="1.7">C-</option> <option value="1.3">D+</option> <option value="1">D</option> <option value="0">F</option> </select> </li> <li>Module 5</li> <li data-role="fieldcontain"> <label for="name5">Credits:</label> <input type="text" name="name5" id="name5" value="" data-clear-btn="true"> </li> <li data-role="fieldcontain"> <label for="grade5" class="select">Grade:</label> <select class="grade5" data-theme="f" id="grade5"> <option value="-1">—</option> <option value="4">A</option> <option value="3.7">A-</option> <option value="3.3">B+</option> <option value="3">B</option> <option value="2.7">B-</option> <option value="2.3">C+</option> <option value="2">C</option> <option value="1.7">C-</option> <option value="1.3">D+</option> <option value="1">D</option> <option value="0">F</option> </select> </li> <li data-role="fieldcontain"> <label for="res">Result:</label> <input type="text" name="res" id="res" value="" data-clear-btn="true"> </li> <li class="ui-body ui-body-b"> <fieldset class="ui-grid-a"> <div> <button type="button" id="cal" name="cal" data-theme="f">Calculate</button> </div> </fieldset> </li> <li class="ui-body ui-body-b"> <fieldset class="ui-grid-a"> <div> <button type="reset" data-theme="d">Reset</button> </div> </fieldset> </li> </ul> </form> </div>
因此,我将spring应用程序上下文配置为:
<extensions>
<property name="initSql">SET SCHEMA 'foo'</property>
<property name="timezone">PDT</property>
</extensions>
然而,没有任何反应,它不会抛出错误,但不会按预期运行。
答案 0 :(得分:2)
你在答案中所做的并不足以使其发挥作用
如果你调查你的mysql日志,你会发现timezone
设置永远不会生效(例如,没有“set time_zone ...”语句执行)。
您的答案中唯一生效的是preferredTestQuery
,您已将其设为alter session set current_schema=MY_SCHEMA
。
这意味着每个 Checkin(几乎在您执行的每个查询之前发生 - 即太多)也会调用alter session set current_schema=MY_SCHEMA
,这是一种非常糟糕的表现习惯...
如果要在获取连接时执行某些SQL,则需要将ConnectionCustomizer
与您创建的extensions
地图结合使用。 (你可以看到它写在他们的文档here)
示例:
public class ExampleConnectionCustomizer extends AbstractConnectionCustomizer {
public ExampleConnectionCustomizer () {
}
private String getInitSql(String parentDataSourceIdentityToken) {
return (String)this.extensionsForToken(parentDataSourceIdentityToken).get("initSql");
}
public void onAcquire(Connection c, String pdsIdt) {
String initSql = this.getInitSql(parentDataSourceIdentityToken);
if(initSql != null) {
Statement stmt = null;
try {
stmt = c.createStatement();
stmt.executeUpdate(initSql);
} finally {
if(stmt != null) {
stmt.close();
}
}
}
}
}
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
....The rest of your properties...
<property name="preferredTestQuery" value="SELECT 1" /> <!--Much more efficient-->
<property name="connectionCustomizerClassName" value="yourpackage.ExampleConnectionCustomizer" />
<!-- extensions -->
<property name="extensions">
<map>
<entry key="initSql" value="alter session set current_schema=MY_SCHEMA" />
</map>
</property>
</bean>
答案 1 :(得分:-1)
编辑:不是个好主意。最好遵循Shay Elkayam的方法
经过大量挖掘后,我想出了这个完全有效的配置:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);