使用 myBatis 和 c3p0 配置用户名/密码
在 j2ee 应用程序的生命周期中,我需要处理 2 个独立的数据连接。事先就知道它的所有属性,并且我将 myBatis 配置为这样,
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jdbc/pooledDS" />
</dataSource>
</environment>
这很棒。 PooledDS 指的是我的 c3p0 配置的数据源。第二个连接将使用用户名/密码组合创建,该组合在用户登录应用程序时确定。我想再次使用 c3p0 作为该数据源,并尝试将 mybatis.xml 配置为
<environment id="user">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jdbc/pooledDS2" />
</dataSource>
</environment>
Tomcat 的 context.xml 中相应的资源条目是
<Resource name="jdbc/pooledDS2" auth="Container"
description="DB Connection for Users"
driverClass="oracle.jdbc.driver.OracleDriver"
maxPoolSize="100" minPoolSize="10" acquireIncrement="1"
factory="org.apache.naming.factory.BeanFactory"
maxIdleTime="850"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:oracle:thin:@localhost:1521:orcl4" />
您看,我将用户和密码属性留空,因为我不知道它们。当我知道需要连接的用户时,我会尝试以下操作:
Reader reader = Resources.getResourceAsReader(RESOURCE);
Properties userProps = new Properties();
userProps.setProperty("user", loginName);
userProps.setProperty("username", loginName);
userProps.setProperty("password", password);
sqlMapperUser = new SqlSessionFactoryBuilder().build(reader, "user", userProps);
您看,当我获取 SqlSessionFactory 时,我尝试将用户名和密码作为 Property 对象传递。当我查看 tomcat 中 c3p0 的日志消息时,我发现 c3p0 属性为空,显然它从未从 myBatis 听到用户名和密码是什么,因此无法建立连接。我知道我正在使用正确的“用户”环境,只是如何正确设置此连接的用户名和密码?感谢您的帮助。
I have 2 separate data connections I need to to handle during the lifecycle of a j2ee application. One has all of its properties known before hand and I configure myBatis as so
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jdbc/pooledDS" />
</dataSource>
</environment>
This is great. PooledDS refers to my c3p0 configured data source. The second connection will be created with a username/password combo which is determined when a user logs in to the application. I would like to use c3p0 again for that data source and I attempt to configure mybatis.xml as
<environment id="user">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jdbc/pooledDS2" />
</dataSource>
</environment>
My corresponding resource entry in my Tomcat's context.xml is
<Resource name="jdbc/pooledDS2" auth="Container"
description="DB Connection for Users"
driverClass="oracle.jdbc.driver.OracleDriver"
maxPoolSize="100" minPoolSize="10" acquireIncrement="1"
factory="org.apache.naming.factory.BeanFactory"
maxIdleTime="850"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:oracle:thin:@localhost:1521:orcl4" />
You see, I leave the user and password attributes blank because I do not know them. When I know the user for whom I need a connection I try the following:
Reader reader = Resources.getResourceAsReader(RESOURCE);
Properties userProps = new Properties();
userProps.setProperty("user", loginName);
userProps.setProperty("username", loginName);
userProps.setProperty("password", password);
sqlMapperUser = new SqlSessionFactoryBuilder().build(reader, "user", userProps);
You see, I try to pass in the username and password as a Property object when I get my SqlSessionFactory. When I look at my log messages in tomcat for c3p0 I see that the c3p0 properties are empty and apparently it never heard from myBatis what the username and password are so it cannot make a connection. I know I am using the correct "user" environment, it is just how do I properly set the username and password for this connection? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想出了我需要做的一切,所以让我分享一下。
我在 mybatis google 邮件列表中的某个地方发现了这个花絮:
不过我很懊恼,我无法像我试图做的那样传递我自己的属性。上面的 setProperties 方法中出现的唯一属性是 mybatis.xml 文件中定义的属性,多么令人失望。
然后我们调整构建的调用如下:
多么令人失望!我必须知道我正在使用哪种数据源。这样就减少了 MyBatis 的一些真正的功能。无论如何,在这里我知道要选择哪个用户名,因此我设置了该特定 c3p0 数据源的 User 属性,现在我可以像往常一样使用 mybatis 打开会话。
最后,“用户”环境是什么样的?这个怎么样?
因此,在这里我省略了要动态设置的用户属性,就像我刚才演示的那样。就是这样,MyBatis 与 c3p0 集成。我认为这表明了 MyBatis 的一个弱点,那就是你必须深入研究 SqlSessionFactory 对象。但话又说回来,如果我选择一个框架,我就会不切实际地期待魔法。对于 MyBatis,我并没有获得魔法。否则,可以采取其他选择来处理持久性。
Ok I figured out everything I need to do so let me share.
I found this tidbit somewhere or another on the mybatis google mailing list:
Much too my chagrin though, I can not pass in my own properties as I was trying to do. The only properties which appear in the above setProperties method are those which are defined in the mybatis.xml file, how disappointing.
Then we adjust the call to build as follows:
How disappointing! I have to know what kind of DataSource I am using. That sort of reduces some of the real meat of MyBatis. Anyway, here I know which username is to be selected so I set the User property of that particular c3p0 datasource and now I can open up sessions with mybatis as usual.
Finally, what does the "user" environment look like? How about this?
So here I have left out the user property to be set dynamically like I just demonstrated. That is it, MyBatis integrated with c3p0. I think this demonstrates a weakness of MyBatis that you have to delve deep down into the SqlSessionFactory object. But then again, if I choose a framework I am unrealistically expecting magic. And with MyBatis I do not get magic. Otherwise, there are other choices which can be made to handle persistance.